...

View Full Version : Select item in dropdown list based on session variable



rookiecoder
10-21-2009, 01:37 PM
Hi ,
So I have a dropdown list such as

<select name=\"ddlwork\" id=\"ddlplace\"> which has items based on values i.e
<option value=\"$work_id\">$work_name</option>

Now, I am passing the value to this page through a session variable and would like this drop down list to show the item whose value is equal to that session variable ?

What do I have to put in my select tag ? Is there something like selectitembyvalue($value) ?

sillyfishyboy
10-21-2009, 01:43 PM
the seleted data goes on the option. so

<option value=\"$work_id\" selected>$work_name</option>
before I have always had to do an if/else on the options


if($_SESSION['value'] == value){
<option value=\"$work_id\" selected>$work_name</option>
}else{
<option value=\"$work_id\">$work_name</option>
}


If anyone knows of a better way, it could be useful though.

Rowsdower!
10-21-2009, 01:45 PM
Hi ,
So I have a dropdown list such as

<select name=\"ddlwork\" id=\"ddlplace\"> which has items based on values i.e
<option value=\"$work_id\">$work_name</option>

Now, I am passing the value to this page through a session variable and would like this drop down list to show the item whose value is equal to that session variable ?

What do I have to put in my select tag ? Is there something like selectitembyvalue($value) ?

You could use an "if" statement in your PHP to test the session variable against the options you have in your select box. Then assign the attribute selected="selected" to the option that you want to have appear as the default when the page is sent to the browser.

rookiecoder
10-23-2009, 11:56 AM
Thanks. Along the same lines how do I hide (do not display) a checkbox if a session variable is set? Is there a tag that I can use such as "hidden=true"

Currently, I have it like this


<input type="checkbox" id="chkusesame" name="chkusesame">

So now I can check for a session variable is set by making use of (isset), and storing the boolean value in a variable.

So is there some thing like a hidden=$boolvalue or display=$boolvalue available for me ?
Or do I need to use div tags around it and then proceed?

Rowsdower!
10-23-2009, 07:21 PM
Thanks. Along the same lines how do I hide (do not display) a checkbox if a session variable is set? Is there a tag that I can use such as "hidden=true"

Currently, I have it like this


<input type="checkbox" id="chkusesame" name="chkusesame">

So now I can check for a session variable is set by making use of (isset), and storing the boolean value in a variable.

So is there some thing like a hidden=$boolvalue or display=$boolvalue available for me ?
Or do I need to use div tags around it and then proceed?

Not really, but you could use a similar PHP session variable check (with an "if" statement) to add style="display:none;" to the tag when that element should be hidden.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum