Link to home
Start Free TrialLog in
Avatar of CHRIS-WK
CHRIS-WK

asked on

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

SOLUTION
Avatar of dolomitedave
dolomitedave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
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

Avatar of CHRIS-WK
CHRIS-WK

ASKER

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.


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!
I keep allot of hair because EE exists! Thank you!
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!
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