I have a form that validates the information submitted within a form, method is POST and is submitted to another file “Check.php”, I am using sessions to remember what the POST data is, when ever there is a problem with the form I redirect it back to the form input page.
How do you get the post data to remember which OPTION was selected in the SELECT input?
CODE:
<select name="referred">
<option value="facebook">facebook</option>
<option value="youtube">youtube</option>
<option value="reddit">reddit</option>
<option value="google">google</option>
</select>
I’ve tried to do this but does not work:
<select name="referred">
<option value="facebook"
<?php
if (isset($_POST['referred']) && $_POST['referred'] == "Facebook") {
print "selected=\'selected\'";
}
?>?> >facebook</option>
I’ve tried to do this but still, does not work 🙁
<select name="referred">
<option value="facebook"
<?php
if (isset($_POST['referred'])) {
if ($_POST['referred'] == "youtube") {
echo "selected=\'selected\'";
} else {
echo "";
}
}
?> >facebook
Please help!
EDIT:
I APOLOGISE, I’m not using the $_POST here to check if it’s set, I should be using the $_SESSION, see below:
<select name="referred">
<option name="google"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "google"){ echo "selected='selected'";}else{ echo "";}}?>>google</option>
<option name="youtube"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "youtube"){ echo "selected='selected'";}else{ echo "";}}?>>youtube</option>
<option name="reddit" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "reddit"){ echo "selected='selected'";}else{ echo "";}}?>>reddit</option>
<option name="facebook" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "facebook"){ echo "selected='selected'";}else{ echo "";}}?>>facebook</option>
</select>
You don’t need to escape single quotes in a string wrapped in double quotes.
Like that your html markup would end up like
<option value="facebook" selected=\'selected'\>Which is bad html.This should work.
EDIT
If you have a known set of options then make an array and loop through them: