Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set value of select statement

Status
Not open for further replies.

tyrannus

MIS
Apr 6, 2006
115
US
I have an application I am building where a user logs on and is directed to a page allowing them to enter in some information to be submitted. When the user registers they can pick their default location and I have that set in a $_SESSION variable. On the page they are going to, I need to have a drop down box with all the location options in it, which I have. What I need to do is when they goto that page, I want the drop down box to default to their default location in the $_SESSION variable..

In a nutshell, drop down box lists all options, but I want the first option to be their default location. Drop down box is built from an array in PHP.
 
Can you simply first add the location from the session variable into your list (semi-hardcode) after verifying that it is a valid location, then when filling the rest of the locations, either drop the value if it matches the default, or simply "select ... from ... where location not = $Session_Location"

Hope this helps

- flub
 
Here is my code for filling the array/option list:
Code:
function Store(){
$qs = "select * from stores order by store_num";
$q = mysql_query($qs);
while ($row = mysql_fetch_array($q))
  {
   echo "<option value='$row[0]'";
   echo ">$row[0]</option>\n";
  }
}

Then there is a select statement in the page that calls this function to display the list.

I want the default option to be equal to the value of $_SESSION['location'].
 
You'll have to dynamically contruct your drop down, and check whether the option currently being printed matches that of your session variable, in which case you add the "SELECTED" to it, so its that one that is preselected in the drop down.

example:

Code:
echo "<select name='locations'>";
foreach($locations as $location){
if($location==$_SESSION['usrlocation']){
echo "<option value=$location [red]selected=selected[/red]> $location</option>";
}
else{
echo "<option value=$location>$location</option>";
}
}
echo "</select>"

Of course this is just an example, how you construct the loop will depend on how you get the available locations. The red part is what makes the option selected by default when the browser renders the form

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That worked great!!! Many thanks... I have another step that needs to be accomplished but I will try to figure that one out on my own first.
 
Glad I could help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top