Jump to content

[SOLVED] pulldown menu for query


wkilc

Recommended Posts

index.php will show all records.

 

I want to query (filter) the database like this:

index.php?car=toyota

or

index.php?car=honda

 

<form name="form" action="index.php" method="get">
<b>Filter cars:</b><br>
<select name="car">
<option value="">DISPLAY ALL</option>
<option value="honda">honda</option>
<option value="toyota">toyota</option>
<option value="ford">ford</option>
</select>
 <input type="submit" value="Go">
</form>

 

It works just fine... the page is filtered correctly.  The only problem is with every submit, the menu defaults back to the first value "DISPLAY ALL". 

 

Is there any simple way to make the pulldown menu "hold" the value that it's currently displaying?

 

Thanks,

 

~Wayne

Link to comment
https://forums.phpfreaks.com/topic/111430-solved-pulldown-menu-for-query/
Share on other sites

<form name="form" action="index.php" method="get">
<b>Filter cars:</b><br>
<select name="car">
<option <?php if(empty($_GET['car'])){ echo "selected=\"selected\""; } ?> value="">DISPLAY ALL</option>
<option <?php if($_GET['car'] == "honda"){ echo "selected=\"selected\""; } ?> value="honda">honda</option>
<option <?php if($_GET['car'] == "toyota"){ echo "selected=\"selected\""; } ?> value="toyota">toyota</option>
<option <?php if($_GET['car'] == "ford"){ echo "selected=\"selected\""; } ?> value="ford">ford</option>
</select>
 <input type="submit" value="Go">
</form>

 

OR

 

<form name="form" action="index.php" method="get">
<b>Filter cars:</b><br>
<select name="car">
<option value="">DISPLAY ALL</option>
<?php
$values = array("Honda","Toyota","Ford");
$num_vals = count($values);
for($i = 1,$i <= $num_vals, $i++){
   if($values[$i] == $_GET['car']){
      echo "<option selected=\"selected\" value=\"$value[$i]\">$value[$i]</option>";
   }else{
      echo "<option value=\"$value[$i]\">$value[$i]</option>";
   }
}
?>
</select>
 <input type="submit" value="Go">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.