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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select box, setting session variables for default selection

Status
Not open for further replies.

newphpbie

Programmer
Oct 17, 2003
110
GB
Hi, I have a select box for the usr to select which criteria they wish to search by. Once a user has selected an option and pushed serach, I would like to set the option as a session varaible so that when the page reloads, it will have the option they chose as the default value.

I've written the code for the select box but I just don't know how to set the default value as the option the user just choose.

Code:
echo '<select name=&quot;searchby&quot;>';
echo '<option value=&quot;first&quot;>First Name</option>';
echo '<option value=&quot;last&quot;>Last Name</option>';
echo '<option value=&quot;extnum&quot;>Ext Num</option>';
echo '<option value=&quot;outlet&quot;>Outlet Ref</option>';
echo '</select><br><br>';

I know all I need to do to set a default value is to add 'SELECTED' to it, but I want this value to be set depending on what the user selects.

Could someone give me an idea of how to get this started?

I know I need a session var, and I need to set it everytime the serach button is pushed. But I dont know how I encorporate it into the select box code.

Please help..

Cheers, Tony
 
Start by starting (or continueing the session
Code:
session_start();
I would put the search values into an array
Code:
$search_values = array(&quot;first&quot;, &quot;last&quot;, &quot;extnum&quot;, &quot;outlet&quot;);
$search_labels = array(&quot;First Name&quot;, &quot;Last Name&quot;, &quot;Ext Num&quot;, &quot;Outlet Ref&quot;);
then check if the session variable has been set
Code:
// set the session varible to the value of $searchby
$_SESSION('search') = $searchby;
echo '<select name=&quot;searchby&quot;>';
// loop through the array outputing the select options
for($i=0; $i<count($search_values); $i++)
{
if ($_SESSION('search') == $search_values[$i])
{
// if this option is what is set in the session variable then make it selected
echo '<option value=\&quot;$search_values[$i]\&quot; SELECTED>$search_labels[$i]</option>\n';
} else {
// else output an ordinary option
echo '<option value=\&quot;$search_values[$i]\&quot;>$search_labels[$i]</option>\n';
}
}

I think that'll work, but I have never used sessions before and I haven't tested this code.... it should help at least.
 
ok, That's helped a little... but it doesn't work and I can't get my head round why...

Any clearer ideas?
 
Ok, well for one thing I used round bracket instead of square bracets for the $_SESSION varibles. Change $_SESSION('blah') to $_SESSION['blah']. My mistake, sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top