Remember <select> Options After Form is Submitted

Hello Experts!

Noob attack here so please be gentle. I'll try to be as specific as possible and supply accurate information in regards to what I need help with.

It is pretty simple, I think...

Let's say I have a form with more than one <select> option. The form will have an error checking script and if it finds an error it will display some error messages etc. I want the selected options to be remembered after the form is submitted with an error ( because if all fields are completed correctly the form will redirect to another page )

I found examples on EE with SESSIONS but some returned errors or were for use with a mysql database or it just didn't work!

A perfect example of what I need is in the following code, note that only one <select> option is printed, I tried duplicating the code and changed some variables but it didn't work, it only printed one set of <select>.


<?php
// Sample found at http://www.kirupa.com/forum/showthread.php?t=294957 and modified

$choices = array ('-Select-' => '-Select-',
'01' => 'B1',
'02' => 'B2',
'03' => 'B3',
'04' => 'B4');

if ( $_POST['submit'] ) {

global $choices;
$defaults = array();

foreach($choices as $key => $choice){
if(isset($_POST['s2']) && ($_POST['s2'] == $key)) $defaults['s2'][$key] = 'selected';
else $defaults['s2'][$choice] = '';
} 
if(!(isset($_POST['s2']) && (array_key_exists($_POST['s2'], $choices)) && $_POST['s2'] != "-Select-")) {
}
}
// After this code or whatever code that works to print all Select options, I'll place another code with custom error checking and if all fields are OK then email results+move to next page
?>

<form method="post">

<b>Print All Select Options</b><br><br>

<!-- Select 1  -->
<b>S1:</b>
<select name="s1">
<option value="">-Select-</option>
<option value="01">A1</option>
<option value="02">A2</option>
<option value="03">A3</option>
<option value="04">A4</option>
</select><br><br>

<!-- Select 2 - the only one that prints after submit  -->
<b>S2:</b>
<?php
print "<select name='s2'>";
foreach($choices as $key => $choice){
print "<option value='{$key}' {$defaults['s2'][$key]}>{$choice}</option>"; 
}
print "</select>";
?>

<!-- Select 3  -->
<b>S3:</b>
<select name="s3">
<option value="">-Select-</option>
<option value="01">C1</option>
<option value="02">C2</option>
<option value="03">C3</option>
<option value="04">C4</option>
</select><br><br>

F1<input type="text" name="test1" value="<?php echo $_POST['test1'] ?>"><br>
F2<input type="text" name="test2" value="<?php echo $_POST['test2'] ?>"><br>
F3<input type="text" name="test3" value="<?php echo $_POST['test3'] ?>"><br>

<br><br>
<input type="submit" value="Proceed" name="submit">
</form>

Open in new window

CHRIS-WKAsked:
Who is Participating?
 
aondConnect With a Mentor Commented:
you can use
$s3=$_POST[s3];


<select name="s3">
<option value="">-Select-</option>
<option value="01" <?if($s3=="01") echo " selected";?>>C1</option>
<option value="02" <?if($s3=="02") echo " selected";?>>C2</option>
<option value="03" <?if($s3=="03") echo " selected";?>>C3</option>
<option value="04  <?if($s3=="04") echo " selected";?>">C4</option>
</select><br><br>
0
 
dolomitedaveConnect With a Mentor Commented:
I would use this i don't think its the best way but it works for me

this code is untested
<select name="s1">
<? $var = $_POST['s1']; 
if (isset($_POST['s1']) == false) {$var ="Select";}?>
<option value=""><? echo $var; ?></option>
<option value="01">A1</option>
<option value="02">A2</option>
<option value="03">A3</option>
<option value="04">A4</option>
</select><br><br>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
A regular <select> only posts one value.  You can use <select multiple="multiple"> to post more than one value.

I'm not entirely sure what you're trying to do but I cleaned up your code to eliminate the errors.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// Sample found at http://www.kirupa.com/forum/showthread.php?t=294957 and modified

$choices = array ('-Select-' => '-Select-',
'01' => 'B1',
'02' => 'B2',
'03' => 'B3',
'04' => 'B4');

$test1 = "0";
if(isset($_POST['test1'])) $test1 = $_POST['test1'];
$test2 = "0";
if(isset($_POST['test2'])) $test1 = $_POST['test2'];
$test3 = "0";
if(isset($_POST['test3'])) $test1 = $_POST['test3'];

if (isset($_POST['submit']) ) {

	global $choices;
	$defaults = array();

	foreach($choices as $key => $choice){
	if(isset($_POST['s2']) && ($_POST['s2'] == $key)) $defaults['s2'][$key] = 'selected';
	else $defaults['s2'][$choice] = '';
	} 
	if(!(isset($_POST['s2']) && (array_key_exists($_POST['s2'], $choices)) && $_POST['s2'] != "-Select-")) {
	}
}
// After this code or whatever code that works to print all Select options, I'll place another code with custom error checking and if all fields are OK then email results+move to next page
?>

<form method="post">

<b>Print All Select Options</b><br><br>

<!-- Select 1  -->
<b>S1:</b>
<select name="s1">
<option value="">-Select-</option>
<option value="01">A1</option>
<option value="02">A2</option>
<option value="03">A3</option>
<option value="04">A4</option>
</select><br><br>

<!-- Select 2 - the only one that prints after submit  -->
<b>S2:</b>
<?php
print "<select name='s2'>";
foreach($choices as $key => $choice){
print "<option value='{$key}' {$defaults['s2'][$key]}>{$choice}</option>"; 
}
print "</select>";
?>

<!-- Select 3  -->
<b>S3:</b>
<select name="s3">
<option value="">-Select-</option>
<option value="01">C1</option>
<option value="02">C2</option>
<option value="03">C3</option>
<option value="04">C4</option>
</select><br><br>

F1<input type="text" name="test1" value="<?php echo $test1; ?>"><br>
F2<input type="text" name="test2" value="<?php echo $test2; ?>"><br>
F3<input type="text" name="test3" value="<?php echo $test3; ?>"><br>

<br><br>
<input type="submit" value="Proceed" name="submit">
</form>

</body>
</html>

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CHRIS-WKAuthor Commented:
Thank you all for answering so quickly!

OK.

Dolomitedave: Tried your suggestion and it works, but it prints the value not the option, I mean after submitting it prints 01,02,03 or 04 insteand of A1, A2, A3 or A4. Also after pressing the submit button again S1 comes back again to its default value.

Aond: Your suggestion seems to work great, but after it prints all <select> options and you want to change let's say S3 from C1 to C4 it goes back to -Select-. Maybe that's a php issue, I'll keep trying or maybe just use it like that!

DaveBaldwin: When a user submits the form I need the <select> options to be remembered, <input> ( test1, test2, test3) fields are not the problem.

Example: User selects a type of car ( S1 ), make ( S2 ) and model ( S3 ), submits the form but he forgets to enter something and the form returns an error. The options he/she selected must be remembered. That's about it.


0
 
CHRIS-WKAuthor Commented:
I found a typo on aond's suggestion and everything echo'es/prints just right!

Changed:

<option value="04  <?if($s3=="04") echo " selected";?>">C4</option>


To this:

<option value="04" <?if($s3=="04") echo " selected";?>>C4</option>

Thank you so much for the help! AWESOME WEBSITE! EE = KICK-ASS!
0
 
CHRIS-WKAuthor Commented:
I keep allot of hair because EE exists! Thank you!
0
 
CHRIS-WKAuthor Commented:
DaveBaldwin, seeing the record your have here on EE I just want to tell you that I wasn't underestimating you when I explained what I needed help for, it seem appropriate to explain. I'm more than positive that with your rank this 'question' of mine is a walk in the park!

Thank you all for your time,
Chris.

Sorry for posting so much!
0
 
Dave BaldwinFixer of ProblemsCommented:
That's ok, it just wasn't clear to me what you wanted the first time.  Now that it is, you picked the better answers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.