Shuly Cohen Persiado
Shuly Cohen Persiado

Reputation: 57

Remembering the selected option after page refreshed

I have a form that gets from the user min size and max size of a shoe and prints on the same page by POST method all of the shoes with this size range.

The form action is leading to the same page so the page is kinda refreshed and gets new values for the POST's variables.

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

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

is still selected of course after page refreshed so it prints the results for X size but the option that is selected is 49.0

My question is how can I change the default option to the one that the user choosed before he submited the form or in other words to $_POST['minSize'] and $_POST['maxSize']

Upvotes: 4

Views: 5173

Answers (4)

txpeaceofficer09
txpeaceofficer09

Reputation: 161

This generates the <option></option> elements and adds "selected" if they are the one that needs to be. This keeps you from having to use 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";

?>

Upvotes: 0

geryjuhasz
geryjuhasz

Reputation: 184

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>

Upvotes: 1

ejfrancis
ejfrancis

Reputation: 3045

If you already have the values in the POST array, then you can just build the form inputs with inline php echoing out the value that the user selected. You could put a conditional a the top of the page that sets the values to defaults if the POST wasn't 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 inline with 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'm using the shorthand <?="hello" ?> which is the same as <?php echo "hello" ?>

Upvotes: 1

Jack Harkness
Jack Harkness

Reputation: 810

You can use JavaScript for 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>

Upvotes: 4

Related Questions