views:

47

answers:

1

Using a dropdown menu, I'm to figure out how to activate SQL query without using a form button. Query triggered automatically on user selection.

The code I have so far uses a button to do the queries. (For now only viewing the "View All" --or view specific product type works. The "order by price" high to low, low to high, will be posted as a different question on this site.)

Form:

        <!--category and price form ------------------------- -->
    <form action="register_script.php" name="frm" method="post">
        <select name="category" id="category">
            <option value="viewall">View All</option>
            <option value="dress">Dress</option>
            <option value="athletic">Athletic</option>
            <option value="sandals">Sandals</option>
       </select>

      <input type="submit" value="Go" />

  </form>



   <form action="register_script.php" name="frm" method="post">

        <select name="price" id="price">
            <option value="lowToHigh">Low to High</option>
            <option value="highToLow">High to Low</option>
       </select>

       <input type="submit" name="orderPrice" value="orderPrice" />

    </form>
    </div>

   <?php
    function categoryList($pUserCat=false) {
    echo "I am in category list" . "<br/>";

    $con = getConnection(); 

     $sqlQuery = "SELECT * from Products";

    if($pUserCat == "athletic") {
       $sqlQuery = "SELECT * from Products
                    WHERE ProductType='athletic'";
    } elseif ($pUserCat == "dress") {
        $sqlQuery = "SELECT * from Products
                    WHERE ProductType='dress'";
    } elseif ($pUserCat == "sandals") { 
            $sqlQuery = "SELECT * from Products
                    WHERE ProductType='sandals'";
    } elseif ($pUserCat == "viewall") {                 
       $sqlQuery = "SELECT * from Products";
    }

    // Execute Query -----------------------------           
        $result = mysqli_query($con, $sqlQuery);
        if(!$result) {
            echo "Cannot do query" . "<br/>";
            exit;
        }

        $row = mysqli_fetch_row($result);
        $count = $row[0];

        if ($count > 0) {
            echo "Query works" . "<br/>";
        } else {
            echo "Query doesn't work" ."<br/>";
        }

          // Display Results -----------------------------

        $num_results = mysqli_num_rows($result);

        for ($i=0; $i<$num_results; $i++) {
            $row = mysqli_fetch_assoc ($result);
           // print_r($row);
          echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
          echo "Price: " . stripslashes($row['Price']);
        }

        // Close connection
        closeConnection($con);

}
?>

Screenshot of UI: alt text

+1  A: 

Try this. On your select HTML item add the following:

onChange = "frm.submit()"
Dante617
like this? <select name="category" id="category" onChange = "frm.submit()"> is that javascript?
jc70
Yup. Javascript that just says "submit the form called "frm". This will kick off whatever you have in the action parameter of the form.
Dante617
do u know if there is a way to just doing it with PHP/SQL?
jc70
but thanks. if i can't get it to work with PHP/SQL, i'll use JavaScript.
jc70
What you're trying to do is modify the client behavior. JavaScript runs on the client, PHP runs on the server. I don't know that there is a way to do it with only PHP, but I wouldn't think so.
Dante617