...

View Full Version : How - Assign Value to a Radio Button with a Session Var



Acute Chaos
08-30-2011, 07:40 PM
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:


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


<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!!!

Fou-Lu
08-30-2011, 08:02 PM
Radio's like multiple checkboxes need to determine their value since they are grouped in several options:


$sSelectedColor = isset($_SESSION['color']) ? $_SESSION['color'] : '';
printf('<input type="color" type="radio" value="Red"%s />Red<br />', (strcmp($sSelectedColor, 'Red') == 0 ? ' checked="checked"' : ''));


Works best if you are looping it to make the determination if you are building the radios from a list.

Acute Chaos
08-31-2011, 07:27 PM
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!!

Fou-Lu
08-31-2011, 08:55 PM
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:


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" />...';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum