Populating a radio button from a SQL Database

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
I've been working on a website for my 10 year class reunion. I've done SOOOO much....

I created a registration page that allows you to select whether or not you can attend. (the point is that even if someone can't attend, I'll be able to get their contact info for future communications)

So..they select radio buttons to select whether or not they can attend...then a button for how many guests they plan on bringing.

I created an attendance update page that allows them to change this... Does anyone have an idea of the right way to do this? I have 2 variables: attend and guests both are INTs.

Do you plan on attending?
<input type="radio" id="attend" name="attend" value="0" <?php if (isset($_POST['attend']) && $_POST['attend'] === '1' ) { echo 'checked="checked"';}?>/>Yes
<input type="radio" id="attend" name="attend" value="1" <?php if (isset($_POST['attend']) && $_POST['attend'] === '2' ) { echo 'checked="checked"'; } ?>/>No
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Do you know how to query your SQL database? Why is attend an int? I don't understand which part you're having trouble with.
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
I just set it up as an integer to make it easier.

I've got a contact form with many address fields, etc... I'm able to post to them in another form. Upon doing so, I can then pre-populate an update page to allow users to see their address and make changes if something isn't right.

::EXAMPLE::
E-mail: <input type="text" name="email"size=45 value="<?php echo $email; ?>"><br>

This prepopulates with the user's email address

That is with fields....so I'm trying to do the same thing with radio buttons.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
if(isset($_POST['attend'])
$newAttend = $_POST['attend'];
else $newAttend = false;

then for your radio buttons


<input type="radio" id="attend" name="attend" value="0" <? if ($newAttend) { echo ' checked';}?>/>Yes
<input type="radio" id="attend" name="attend" value="1" <? if ($newAttend) { echo ' checked'; } ?>/>No



I'm pretty sure that in php, when comparing vars and values, quotes around the value casts it as a string
so if you're loading the post array with ints, and you use the === (which must match value AND type), it'll return false with the quotes (again, i could be wrong...i don't feel like looking it up)

so instead, trap for the lack of $_POST['attend'] (should use key_exists instead of isset, but isset is nice and short)
if its set, 1 will be true, 0 will be false, if its not set - it defaults to false
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
One clarification: The code works to post data, but it's not setting the radio buttons when you load the page. After changing the values and hitting submit, the radio buttons flash and display the last value set and not the current one. If I refresh the page, all radio buttons are empty.

If anyone has any ideas I would appreciate anything. I'm going to keep reading and see if I can figure this out. I was just hoping someone would have some experience doing this. Thanks,

-Scar
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
look what i wrote about the === and the quotes

and then at LEAST change your html to be just CHECKED (not checked=checked)
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
I'm confused as to why the value attributes defined on your radio buttons have values of 0 and 1 where 0 is for "Yes" and 1 is for "No". . this seems counter-intuitive. Isn't it usually the other way around? And then why do you check for post values of 1 and 2 instead of 0 and 1?

This would make more sense to me:

<input type="radio" id="attend" name="attend" value="1"<?php if (isset($_POST['attend']) && $_POST['attend'] === '1' ) { echo ' checked '; } ?>/>Yes
<input type="radio" id="attend" name="attend" value="0"<?php if (isset($_POST['attend']) && $_POST['attend'] === '0' ) { echo ' checked '; } ?>/>No

Also what troy said makes sense about the === operator. . .I don't know whether or not in PHP all $_POST variables are always string types. But it's worth considering.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
i didn't even notice the 0 being yes and the 1 being no, haha...i must have been drunk or something


here's an example of what i'm talking about with the ===

<?
$a = 1;

if ($a === '1')
echo "a ==='1'<br>"; // this will be false, because $a is an int, and the quotes around it forces a string comparison. the triple equal sign compares value AND data type int != string

if ($a == '1')
echo "a = = '1'<br>"; // this will be true, even though the '1' is a string, it still holds the same value. double equal sign compares values

if ($a === 1)
echo "a = = = 1<br>"; // this will be true. both are ints. and both have the same value

if ($a == 1)
echo "a = = 1<br>"; // this will be true. both have the same value.
?>

its not so much the value of attend in the post array, its how you're comparing the 0s and 1s to check value
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
Ok it's working now...it turns out that it wasn't working and I never figured out why. It has something to do with the variables...you'd think it'd be the same. Who knows... :p

I started with this:
<input type="radio" id="attend" name="attend" value="0" <?php if (isset($_POST['attend']) && $_POST['attend'] === '1' ) { echo 'checked="checked"';}?>/>Yes

And it started working when I changed it to this:

$attend = $_POST['attend'];

<input type="radio" id="attend" name="attend" value="0" <?php if (isset($attend) && $attend === '1' ) { echo 'checked="checked"';}?>/>Yes


BTW....thanks for the replies. I've made most of the changes and the site is like 90% complete. The majority of what's left is the content. (aka - the easy part)
 

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
I'm not even sure why you are using $_POST. Is this a post page? (a page you get to after using a form?)

 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
Originally posted by: Hyperblaze
I'm not even sure why you are using $_POST. Is this a post page? (a page you get to after using a form?)
Short answer: Yes.