Activate SQL query without using a form button using PHP and not JavaScript
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" m开发者_JAVA百科ethod="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:

Try this. On your select HTML item add the following:
onChange = "frm.submit()"
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论