-1

How to display form inputs without loosing data entered before refresh the page? I mean After refresh my page I want to display all the values entered in forms.

I have 2 inputs One is select option and one is text.

 <input type="text" name="worked_month" value="<?php echo $_SESSION['worked_month']; ?>" />
 <select name="sex">
     <option value="">Select Sex</option>
     <option value="male">Male</option>
     <option value="female">Female</option>
 </select>

I am using following PHP code to display text enter before I refresh the page

isset($_POST['worked_month'])?$_SESSION['worked_month'] = $_POST['worked_month']:$_SESSION['worked_month']="";

It works fine but don't know how to select the option that are selected before refresh. But I don't have to always select same default value. User can select any value

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Mr. Shrestha
  • 102
  • 2
  • 9

3 Answers3

4

Explanation. For each option, check if the post variable matches the value and then use selected attribute to select the matched option.

<select name="sex">
  <option value="">Select Sex</option>
  <option <?php echo isset($_POST['sex']) && $_POST['sex']=='male'? 'selected="selected"' = '' ?> value="male">Male</option>
  <option <?php echo isset($_POST['sex']) && $_POST['sex']=='female'? 'selected="selected"' = '' ?> value="female">Female</option>
</select>
Abdul Mannan
  • 1,072
  • 12
  • 19
  • Why should the OP "do it like this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Mar 28 '16 at 12:15
  • 2
    @JayBlanchard thnx for pointing it out. added an explanation. – Abdul Mannan Mar 28 '16 at 12:18
  • This works only if the action is done in the same file.. otherwise there should be used sessions.. – Mihai Matei Mar 28 '16 at 12:19
0

To select option you have to use PHP dynamic variable.Check this reference

<?php
$sex = $_SESSION['sex'];

${$sex.'_checked'} = "selected";

?>

<select name="sex">
<option value="">Select Sex</option><option value="male" <?php echo $male_checked; ?> >Male</option><option value="female" <?php echo $female_checked; ?>>Female</option></select>

Dynamic variable: automatic convert your selected value into variable whose value is "selected".

0

I just made something like that saving the values at Local Storage and then retrieving them:

//Saving the input values at local storage
var temp = [];
$('.keep-values').each(function(){
    temp.push({
      id:$(this).attr('id'),
      value:$(this).val().trim(),
      checked:$(this).is(':checked')
    });
});
localStorage['valuesCache'] = JSON.stringify(temp);

Then i retrive the values and i populate the fields:

//Retrieving the values from local Storage and populating the inputs
var tmp = JSON.parse(localStorage['valuesCache']);
for(i in tmp) {
  $('#'+tmp[i].id).val(tmp[i].value);
  if(tmp[i].checked){
    $('#'+tmp[i].id).attr('checked','');
  }
}

I think thats a good start for your final solution, read more about Local Storage

Marvin Medeiros
  • 202
  • 4
  • 22