Remembering the selected option after refreshing the page

I have a form that receives the minimum size and maximum size of shoes from the user and prints all shoes with this size range on the same page with the POST method.

The action of the form leads to the same page, so the page is updated and receives new values ​​for the POST variables.

My problem is that the default selection option (for example)

<option value="49.0" selected="selected">49.0</option> 

still selected after refreshing the page, so it prints the results for size X, but the selected option is 49.0

My question is how to change the default setting to the one that the user selected before he submitted the form or, in other words, to $ _POST ['minSize'] and $ _POST ['maxSize']

+4
source share
4 answers

You can use JavaScript to do this:

 <script type="text/javascript"> document.getElementById('idofselectlist').value = "<?php echo $_POST['minSize'];?>"; </script> 

and

 <script type="text/javascript"> document.getElementById('idofselectlist').value = "<?php echo $_POST['maxSize'];?>"; </script> 
+4
source

If you already have values ​​in the POST array, you can simply create form inputs with built-in php displaying the value selected by the user. You can put a conditional value at the top of the page, which sets default values ​​if POST has not been set.

 //check if max size is set via post if(!isset($_POST['maxsize'])){ $maxSize = 10; }else{ $maxSize = $_POST['maxsize']; } //check if min size is set via post if(!isset($_POST['maxsize'])){ $minSize= 1; }else{ $maxSize = $_POST['minsize']; } 

then paste your html use this

 <form action="form_handle.php"> Min shoe size: <input type="text" name="min" value="<?=$minSize; ?>"><br> Max shoe size: <input type="text" name="max" value="<?=$maxSize; ?>"><br> <input type="submit" value="Submit form"> </form> 

I use shorthand <?="hello" ?> , Which is the same as <?php echo "hello" ?>

+1
source

It is not the most elegant, but it works

  <?php if(isset($_POST['min'])) $selected = true; ?> <form action="test.php" method="POST"> <select name="min"> <option value="49.0" <?php if(isset($selected) && $selected && $_POST['min']=='49.0') echo 'selected="selected"'?>>49.0</option> <option value="48.0" <?php if(isset($selected) && $selected && $_POST['min']=='48.0') echo 'selected="selected"'?>>48.0</option> <option value="47.0" <?php if(isset($selected) && $selected && $_POST['min']=='47.0') echo 'selected="selected"'?>>47.0</option> <option value="46.0" <?php if(isset($selected) && $selected && $_POST['min']=='46.0') echo 'selected="selected"'?>>46.0</option> <option value="45.0" <?php if(isset($selected) && $selected && $_POST['min']=='45.0') echo 'selected="selected"'?>>45.0</option> </select> <input type="submit" value="Press"> </form> 
+1
source

This generates <option></option> elements and adds "selected" if this is the one that should be. This prevents you from using javascript, which your clients may block.

 $minSize = ( isset($_REQUEST['minSize']) ) ? $_REQUEST['minSize'] : 10; $maxSize = ( isset($_REQUEST['maxSize']) ) ? $_REQUEST['maxSize'] : 50; echo "<select name=\"minSize\">\n"; for ($i=10;$i<=50;i++) { echo "\t<option value=\"".$i."\"".(($i==$minSize) ? " selected" : "").">".$i."</option>\n"; } echo "</select>\n<select name=\"maxSize\">\n"; for ($i=10;$i<=50;i++) { echo "\t<option value=\"".$i."\"".(($i==$maxSize) ? " selected" : "").">".$i."</option>\n"; } echo "</select>\n"; ?> 
0
source

Source: https://habr.com/ru/post/1486884/


All Articles