Ansh
Ansh

Reputation: 261

How to keep value selected after form submit in mysql populated dropdown values?

I am trying to list some data after dropdown selection and submit button press. Further I want the selection made to be displayed on the dropdown box even after form submit. The values appearing on the selectbox are coming from mysql dynamically. I have tried something but things are not working out. Any insight will be quite helpful. Following is my code that I am trying,

<select style="width:250px;" name="batch_number" class="form-control" id="user_id" required="">
  <option value="">Select Batch number---Org.</option>
      <?php

      $batch_sql=mysql_query("select batch, batch_next tbl order by date desc");
         while ($batch_data=mysql_fetch_array($batch_sql))
               {
                  $batch = $batch_data['batch'] ;
                  $batch_next = $batch_data['batch_next'] ;
      ?>
       <option value="<?php echo $batch;?><?php echo (isset($_REQUEST['batch']) && $_REQUEST['batch'] == ''.$batch.'') ? 'selected="selected"' : ''; ?>"><?php echo $batch."---".$batch_next;?></option>
      <?php } ?>

    </select>

Upvotes: 1

Views: 36

Answers (1)

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

Try to change your option value as below.

<option value="<?php echo $batch;?>"

<?php echo (isset($_REQUEST['batch']) && $_REQUEST['batch'] == $batch) ? "selected" : ''; ?>>

<?php echo $batch."---".$batch_next;?>

</option>

Upvotes: 1

Related Questions