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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How - Assign Value to a Radio Button with a Session Var

    I have a members' site and on the admin's page where they update the member info I have created a form with a field for each piece of information.

    Each field is filled in with information drawn from session vars.

    For example the email field has this:

    Code:
    <input name="email" type="text" id="email" value="<?php echo $_SESSION['email'];?>" />
    Works perfect. But I don't know how to assign a value to a radio button so it has an appropriate preselected value.

    How do the same kind of thing with my session info for this:

    Code:
    <input name="color" type="radio" value="Red" />Red <br />
    <input name="color" type="radio" value="Blue" />Blue
    Meaning if the session shows the database has 'Red' as the current value how can I make that the preselected value when the form displays.

    Thanks!!!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Radio's like multiple checkboxes need to determine their value since they are grouped in several options:
    PHP Code:
    $sSelectedColor = isset($_SESSION['color']) ? $_SESSION['color'] : '';
    printf('<input type="color" type="radio" value="Red"%s />Red<br />', (strcmp($sSelectedColor'Red') == ' checked="checked"' '')); 
    Works best if you are looping it to make the determination if you are building the radios from a list.

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have to check the manual to understand a bit of the syntax but the concept is straight forward.

    I have a few things left on my list to knock off and that was one that was really bugging me.

    Thank you so much. Moving On!!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Acute Chaos View Post
    I have to check the manual to understand a bit of the syntax but the concept is straight forward.

    I have a few things left on my list to knock off and that was one that was really bugging me.

    Thank you so much. Moving On!!
    You're welcome.
    Its likely the ternary operator that you had to look up. The above is equivalent to:
    PHP Code:
    if (isset($_SESSION['color']) && strcmp($_SESSION['color'], 'Red') == 0)
    {
        print 
    '<input type="radio" name="color" value="Red" checked="checked" />...';
    }
    else
    {
        print 
    '<input type="radio" name="color" value="Red"  />...';



  •  

    Posting Permissions

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