?
Solved

Remember <select> Options After Form is Submitted

Posted on 2010-09-05
8
Medium Priority
?
766 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:CHRIS-WK
8 Comments
 
LVL 1

Assisted Solution

by:dolomitedave
dolomitedave earned 400 total points
ID: 33607680
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
 

Accepted Solution

by:
aond earned 1600 total points
ID: 33607695
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 33607713
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CHRIS-WK
ID: 33607873
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
 

Author Comment

by:CHRIS-WK
ID: 33607885
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
 

Author Closing Comment

by:CHRIS-WK
ID: 33607898
I keep allot of hair because EE exists! Thank you!
0
 

Author Comment

by:CHRIS-WK
ID: 33607934
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 33608121
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question