user1818407
user1818407

Reputation:

When I select drop down values from database table then page refresh and value get back to select option

When I select drop-down values from a database table and then refresh the page, the value gets back to select option

Here is my code:

<select name="customers" id="customers" style="width:150px;"  >
    <option value=""><--Select--></option>
    <?php 
        $query=mysql_query("SELECT cn FROM customerinformation order by id");
        while($row=mysql_fetch_assoc($query))
        {
             $val2=$row['cn'];
    ?>
        <option  value="<?=$val2;?>"
            <?if ($_REQUEST['cn'] == $val2){ echo "selected='selected'"; }?>> 
            <?=$row['cn'];?> 
        </option>
    <?php }?>
</select>

Please tell me or guide me where I'm doing wrong. Thanks in advance!

Upvotes: 1

Views: 477

Answers (2)

mychalvlcek
mychalvlcek

Reputation: 4046

i dont understand your question but i think you should put $_REQUEST['customers'] instead of $_REQUEST['cn'] (because of name of your select) example:


<select name="customers" id="customers" style="width:150px;"  >
<option value=""><--Select--></option>
  <?php

  $query=mysql_query("SELECT cn FROM customerinformation order by id");
  while($row=mysql_fetch_assoc($query)) {
      $selected = ($_REQUEST['customers'== $row['cn']?'selected="selected"':'');
      echo '<option value="'.$row['cn'].'" $selected>'.$row['cn'].'</option>';
  }

  ?>
</select>

Upvotes: 1

Teena Thomas
Teena Thomas

Reputation: 5239

You need to unset() the value in $_REQUEST['cn'], once its used for evaluation,

    <? if ($_REQUEST['cn'] == $val2) { 
        echo "selected='selected'"; 
        unset($_REQUEST['cn']); 
    }?> > <?=$row['cn'];?> </option>

Upvotes: 0

Related Questions