By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

How to maintain the users list box selection on post?

P: 3
Hi Guys

I am trying to maintain the users list box selection after posting.
How can I get this script to work?
Is there perhaps a better way of doing this?

<?php

$mySelectOption['1'] =
$mySelectOption['2'] =
$mySelectOption['3'] =
$mySelectOption['4'] =
$mySelectOption['5'] =
$mySelectOption['6'] = '';

if(!empty($_REQUEST['mySelect'])){
$mySelectOption[$_['mySelect']] = 'selected';
}else {
$mySelectOption[1] = 'selected'; // Default selected, for first time
}
?>

<form name="myform" action="mySelectOption.php">
<select name="mySelect">
<option value="1" <?=$mySelectOption['1']?> >option1</option>
<option value="2" <?=$mySelectOption['2']?> >option2</option>
<option value="3" <?=$mySelectOption['3']?> >option3</option>
<option value="4" <?=$mySelectOption['4']?> >option4</option>
<option value="5" <?=$mySelectOption['5']?> >option5</option>
<option value="6" <?=$mySelectOption['6']?> >option6</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
Feb 22 '08 #1
Share this Question
Share on Google+
3 Replies


P: 3
I sort of managed to get it working like this, but only if I the value is in a specific numerical order. ...Is there a better solution?
[php]
<?php
if(isset($_POST[mySelect])){
$mySelect = $_POST[mySelect];

switch($mySelect){
case 1:
$mySelectOption[1] = 'selected';
break;
case 2:
$mySelectOption[2] = 'selected';
break;
case 3:
$mySelectOption[3] = 'selected';
break;
case 4:
$mySelectOption[4] = 'selected';
break;
case 5:
$mySelectOption[5] = 'selected';
break;
case 6:
$mySelectOption[6] = 'selected';
break;
default:
$mySelectOption[1] = 'selected';
}
}
?>

<form name="myform" method="post" action="mySelectOption2.php">
<select name="mySelect" id="mySelect">
<option value="1" <?=$mySelectOption['1']?> >option1</option>
<option value="2" <?=$mySelectOption['2']?> >option2</option>
<option value="3" <?=$mySelectOption['3']?> >option3</option>
<option value="4" <?=$mySelectOption['4']?> >option4</option>
<option value="5" <?=$mySelectOption['5']?> >option5</option>
<option value="6" <?=$mySelectOption['6']?> >option6</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>[/php]
Feb 22 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Feb 22 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Best way to do is is to echo the 'selected' attribute, after testing the $_POST, in your form. When the $_POST is not set, iut is the first pass and option 1 will be set to selected (default).

See this code:
[php]<?php
if(isset($_POST[mySelect])){
$mySelect = $_POST[mySelect];

switch($mySelect){
case 1 : // process
break;
case 2 : // process
break;
case 3 : // process
break;
case 4 : // process
break;
case 5 : // process
break;
case 6 : // process
break;
default: // process
break;
}
}
?>
<form name="myform" method="post" action="a.php">
<select name="mySelect" id="mySelect">
<option value="1" <?php if(!isset($_POST['mySelect']) OR $_POST['mySelect']==1) echo "selected"?> >option1</option>
<option value="2" <?php if($_POST['mySelect']==2) echo "selected"?>>option2</option>
<option value="3" <?php if($_POST['mySelect']==3) echo "selected"?> >option3</option>
<option value="4" <?php if($_POST['mySelect']==4) echo "selected"?> >option4</option>
<option value="5" <?php if($_POST['mySelect']==5) echo "selected"?>>option5</option>
<option value="6" <?php if($_POST['mySelect']==6) echo "selected"?> >option6</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>[/php]
Ronald
Feb 22 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.