I have a form named reg.php and its action is reg.php , i want the selected dropdown value when clicked on submit to remain selected , what i have done so far is given below
I have checked the questions that are posted but i couldnt match it with my code
<?php
if( $_POST['registerbtn']){
$selected_value = $_POST['selectID'];
$query = mysql_query("SELECT linecard_name FROM selection WHERE select_id = '$selected_value'");
$rows=mysql_fetch_assoc($query);
$linecard_name= $rows['linecard_name'];
$sql = "SELECT select_id, linecard_name FROM selection " . "ORDER BY linecard_name";
$rs = mysql_query($sql);
while($rownw = mysql_fetch_array($rs)){
if( $rownw['linecard_name'] == $linecard_name) {
$options = "<option selected =selected value=".$rownw['select_id']."> " .$rownw['linecard_name']. " </option> ";
}
}
}
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
mysql_close() ;
$form = "<form action='./reg.php' method='post'>
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID' > <option value = '0'> Select from here </option> $options </select></td>
</tr>
<tr>
<td > <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</table>
echo $form;
The select statements before the register button is clicked is for filling the dropdown from the database,
I just want the selected items to remain selected when the particular selection is done by the user and register button is clicked ,
in which case my code fills the dropdown again but now with the user selected item as selected but without clearing the previous items filled in the dropdown i.e before the register button is clicked,
Please help
I find that it’s cleanest to do it with a variable, like this: