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

前端 未结 8 1939
無奈伤痛
無奈伤痛 2021-01-03 15:33

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 Pos

相关标签:
8条回答
  • 2021-01-03 15:59

    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>
    
    0 讨论(0)
  • 2021-01-03 16:05

    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>";
    }
    ?>
    0 讨论(0)
提交回复
热议问题