0

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

2
  • This is not a SQL question, this is a javascript question: how do you submit a form without a submit button? Commented Sep 21, 2010 at 20:25
  • i've been told that it is possible, and have also seen other forums online of people trying it out. i'm trying to see if i can do it too. Commented Sep 21, 2010 at 20:35

1 Answer 1

1

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

onChange = "frm.submit()"
Sign up to request clarification or add additional context in comments.

5 Comments

like this? <select name="category" id="category" onChange = "frm.submit()"> is that javascript?
Yup. Javascript that just says "submit the form called "frm". This will kick off whatever you have in the action parameter of the form.
do u know if there is a way to just doing it with PHP/SQL?
but thanks. if i can't get it to work with PHP/SQL, i'll use JavaScript.
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.

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.