Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Select item in dropdown list based on session variable

    Hi ,
    So I have a dropdown list such as
    Code:
    <select name=\"ddlwork\" id=\"ddlplace\">
    which has items based on values i.e
    Code:
    <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) ?
    Last edited by rookiecoder; 10-21-2009 at 01:39 PM.

  • #2
    New Coder
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    the seleted data goes on the option. so
    Code:
    <option value=\"$work_id\" selected>$work_name</option>
    before I have always had to do an if/else on the options
    Code:
    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.

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by rookiecoder View Post
    Hi ,
    So I have a dropdown list such as
    Code:
    <select name=\"ddlwork\" id=\"ddlplace\">
    which has items based on values i.e
    Code:
    <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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

    Code:
    <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?

  • #5
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by rookiecoder View Post
    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

    Code:
    <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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    rookiecoder (10-24-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •