1

Hello everyone I'm new to php, mysql, and html and for a project I'm working on have a simple database with id, product, price, and condition. On my html form I have four checkboxes with ranges of prices 0-25, 25-50, 50-75, and 75-100. Here's an example of what I'm trying to do: if the user selects the checkbox with 0-25, it's supposed to echo the id, product, price, and condition of all products that cost 0-25 in a table.I have the checkbox values saved in an array, but I'm completely lost at this point. How can I output values in my database of the checkbox checked.

P.S. I've heard of PDO, but we have to use mysql for this project. Here's my html:

<form action="pricefilter.php" method="post">
<br><b>Filter By Price:</b><br><br> 
<input type="checkbox" name="priceFilter[]" id="Price" value="025"/>&nbsp;$0-$25<br><br>
<input type="checkbox" name="priceFilter[]" id="Price" value="2550"/>&nbsp;$25-$50<br><br>
<input type="checkbox" name="priceFilter[]" id="Price" value="5075"/>&nbsp;$50-$75<br><br>
<input type="checkbox" name="priceFilter[]" id="Price" value="75100"/>&nbsp;$75-$100<br><br>
<input type="checkbox" name="priceFilter[]" id="Price" value="100"/>&nbsp;$75-$100<br><br>
<input type="submit" name="submit" value="Submit" />
</form>

Here is my php:

<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("XUSWAPSAMPLE");

$priceFilter = $_GET['priceFilter'];

 $filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);

    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 5075)
    {
        $query = "select * from Books where Price >= 50 AND Price <=75";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

     if($range == 75100)
    {
        $query = "select * from Books where Price >= 75 AND Price <=100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 100)
    {
        $query = "select * from Books where Price >= 100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

1 Answer 1

1

Two things wrong in your code

1) You are using post method submit form but trying to get value using $_GET, hence you should use $_POST

2) You should use isset property of php to check if values exist or not.

So the solution is try this

if (isset($_POST['priceFilter']) && ($_POST['priceFilter']!="")) {
    $priceFilter = $_POST['priceFilter'];

    // put your remaining code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.