简体   繁体   中英

How to Keep the selected value of the select box after Form POST or GET

Im trying to implement the search feature in my website.

when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.

what i want is to keep the selected category of the combo by default in the form after posted

For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated

I assume you get categories from database.

you should try:

<?php

$categories = $rows; //array from database
foreach($rows as $row){
     if($row['name'] == $_POST['category']){
          $isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
     } else {
          $isSelected = ''; // else we remove any tag
     }
     echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>

Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":

When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.

Here is the JQuery way I am using.

<select name="name" id="name">
  <option value="a">a</option>
  <option value="b">b</option>
</select>

<script type="text/javascript">
   $("#name").val("<?php echo $_POST['name'];?>");
</script>

But this is only if you have jquery included in your webpage. Regards

<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
   <option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
   <option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>

This solved my problem.

$countries_uid = $_POST['countries_uid'];
                while($row = mysql_fetch_array($result)){
                  $uid = $row['uid'];
                  $country = $row['country_name'];
                  $isSelected = null;
                    if(!empty($countries_uid)){
                    foreach($countries_uid as $country_uid){//cycle through country_uid
                       if($row['uid'] == $country_uid){
                        $isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
                       }
                    }
                   }else {
                    $isSelected = ''; // else we remove any tag
                  }
                  echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
                }

this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes

After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.

<?php
$example = $_POST["example"];
?>
<form method="post">        
<select name="example">
    <option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
    <option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
    <option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>

This Solved my Problem. Thanks for all those answered

 <select name="name" id="name">
  <option value="a">a</option>
  <option value="b">b</option>
 </select>

<script type="text/javascript">
  document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>

Easy solution: If select box values fetched from DB then to keep selected value after form submit OR form POST

 <select name="country" id="country">
        <?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
        <option value="">
        <?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
        </option>
        <?php foreach($countries as $country){  ?>
        <option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
        </option>
        <?php } ?>
      </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM