0

I try to create a form with text field i want to keep the value after submit

for that i do this

<input id="datepicker" name="txt_date" type="text" placeholder="Date" class="form-control input-md" value="<?php echo isset($_POST['txt_date']) ? $_POST['txt_date'] : ''?>">

<?php  
    $sql=mysqli_query($conn,"select DISTINCT db_name,db_user from tbl_staff {$query} order by db_name asc ")or die(mysqli_error($conn));
   echo "<select name='txt_name' class='states'>";
   echo "<option value='";?><?php echo isset($_POST['txt_name']) ? $_POST['txt_name'] : ''?><?php echo"'></option>"; 
   while($res=mysqli_fetch_array($sql)){
   $userid=$res['db_user'];
   $name=$res['db_name'];
       if($name!=""){
 echo "<option value='$userid'>$name</option>";
      }} echo "</select>";?>

this code work for the textbox but it didn't work for the select one

when i choose a date and a name from the select menu and do submit only the value in text box stay but on select menu didn't stay

and i have two button one to submit the form and the other to clear the form from the values but also the button clear didn't work

 <input type="submit" name="submit" value="Search" class="btn btn-default">
  <input type="reset" name="clear" value="Clear" class="btn btn-default">

How can i solve this two problems ??!

m7md
  • 231
  • 3
  • 11

1 Answers1

0

You need to set SELECTED as a parameter in the option field that was selected to make it selected. For example:

<option value='$userid' SELECTED>
Sitethief
  • 480
  • 5
  • 17
  • and when i put selected='selected' it didn't keep the value i choose but onther value in the select menu – m7md Nov 29 '16 at 10:15
  • You should add functionality behind that button. See here: http://stackoverflow.com/questions/6028576/how-to-clear-a-form – Sitethief Nov 29 '16 at 10:16
  • You need to add an if statement in your loop so that **only** the correct value gets the SELECTED parameter, otherwise _all_ the options will have that parameter. – Sitethief Nov 29 '16 at 10:17
  • How the if statement should be can you please post an example ?! – m7md Nov 29 '16 at 10:21
  • any suggestion ?!! – m7md Nov 29 '16 at 10:45