Solved

Remember <select> Options After Form is Submitted

Posted on 2010-09-05
8
664 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 100 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 400 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 83

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
Technology Partners: 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 83

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

733 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