473,414 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to maintain the users list box selection on post?

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
3 1858
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
4,258 Expert 4TB
Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Feb 22 '08 #3
ronverdonk
4,258 Expert 4TB
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

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

Similar topics

3
by: Mark R | last post by:
I have one .asp page with a SELECT pulldown list on it and some INPUT fields. When SUBMIT is clicked the form data is submitted to that same page and validated. If INPUT fields are empty the asp...
5
by: William Wisnieski | last post by:
Hello Everyone, I have a query by form with several list boxes. The user selects items from the list boxes and clicks a button that returns results in a datasheet subform. One of the list...
2
by: Danny | last post by:
I have a combo box look up and list items in a table, it is not bound. It works fine but how can I prevent the users from entering in there own data? i have an 'on change' event that when it...
1
by: (Pete Cresswell) | last post by:
I'm using a subform as a navigation list. On the left side of the screen is a subform containing people's names. The right side of the screen is loaded with information about the...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
4
by: anonymous | last post by:
Hi Folks, I have a form with two Dropdown list boxes, which get loaded with data from Database. DropDownList1 gets data from Table1 and DropDownList2 gets data from Table2 Table1 has a...
1
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.