By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Radio Buttons answers not going into MySQL

P: 18
Hope someone can help me out with this.
I'm having trouble getting my radio button values into a MySQL database. And after I do get them inserted I'm going to want to be able to show them back in the form again (if the respondent didn't finish the whole form the first time). My text and textarea fields are inserting just fine and I'm able to bring those back up too. It's just the radio buttons that aren't working. When I created the table I used VARCHAR. Was that even right?


These are the radio buttons in my form:

[HTML]
1st Question:
<p><input type="radio" name="QA" value="AA"> 1st Choice</p>
<p><input type="radio" name="QA" value="AB"> 2nd Choice</p>
<p><input type="radio" name="QA" value="AC"> 3rd Choice</p>
<
<p></p>

<p>2nd Question:</p>
<p><input type="radio" name="QB" value="BA"> 1st Choice</p>
<p><input type="radio" name="QB" value="BB"> 2nd Choice</p>
<p><input type="radio" name="QB" value="BC"> 3rd Choice</p>[/HTML]

These are the instructions on the page where I'm updating them into a table called "XXX". $_SESSION['passcode' comes from a signup page. On that page I've got the INSERT code to get their name and other info. I also insert blank spaces for all the other fields I'll be needing.


[PHP]$AA = $_POST['AA'];
$AB = $_POST['AB'];
$AC = $_POST['AC'];
$BA = $_POST['BA'];
$BB = $_POST['BB'];
$BC = $_POST['BC'];


mysql_query("UPDATE XXX SET AA='$AA', AB='$AB', AC='$AC', BA='$BA', BB='$BB', BC='$BC' WHERE passcode='{$_SESSION['passcode']}'")
or die(mysql_error()); [/PHP]

I'm not sure how to select the radio buttons because I haven't been able to test it since I can't get the data into the database.
Many thanks.
Oct 31 '07 #1
Share this Question
Share on Google+
7 Replies


Atli
Expert 5K+
P: 5,058
Hi.

I think you are misunderstanding how radio buttons work in HTML.

Using your form, even tho you have 6 buttons, only 2 values will be sent.
Only a single radio button out of every radio button in the form sharing a name will be used. So for this form, a value for 'QA' and 'QB' will be sent, each of those containing the value of the selected radio button.

If you want a specific radio button to be selected by default, you should be able to simply add "selected=true" to the <input> tag.
Oct 31 '07 #2

P: 18
Thanks for responding. I've worked at it and now have the values going to my database. Now I need to pull them back into my form when I open it again. I tried the following but it isn't working.



[PHP]
switch($QA)
{
case "QA": $checkAA = "checked"; break;
case "QA": $checkAB = "checked"; break;
case "QA": $checkAC = "checked"; break;
}

switch($QB)
{
case "QB": $checkBA = "checked"; break;
case "QB": $checkBB = "checked"; break;
case "QB": $checkBC = "checked"; break;
} [/PHP]


[HTML]
1st Question:
<p><input type="radio" name="QA" value="AA" <?=$checkAA?>> 1st Choice</p>
<p><input type="radio" name="QA" value="AB" <?=$checkAB?>> 2nd Choice</p>
<p><input type="radio" name="QA" value="AC" <?=$checkAC?>> 3rd Choice</p>

<p></p>

<p>2nd Question:</p>
<p><input type="radio" name="QB" value="BA" <?=$checkBA?>> 1st Choice</p>
<p><input type="radio" name="QB" value="BB" <?=$chechBB?>> 2nd Choice</p>
<p><input type="radio" name="QB" value="BC" <?=$checkBC?>> 3rd Choice</p>[/HTML]

Any hints you can give?
Nov 2 '07 #3

Atli
Expert 5K+
P: 5,058
Hi.

Check out the values you use in your switch statements. You are using the same values for all cases in both of them ("QA" and "QB"). Which probably means that AA and BA will always be selected.

Also, just a note, the <?=$var?> syntax is very dangerous. It's a part of the short-tag deal (<?..?>) which is disabled by default so it's very probable that you will run into problems if you try to use this code on any other server than the one you develop this on.
I'd recommend always using <?php...?>.
Replacing <?=$var?> with <?php echo $var; ?>
Nov 2 '07 #4

P: 18
Hi Atli,
I still can't get it. This is want I have now. (Just the 1st question)

[PHP]switch($QA)
{
case "AA": $checkAA = "checked"; break;
case "AB": $checkAB = "checked"; break;
case "AC": $checkAC = "checked"; break;

}[/PHP]

[HTML]1st Question:
<p><input type="radio" name="QA" value="AA" <?php echo $checkAA;?>> 1st Choice</p>
<p><input type="radio" name="QA" value="AB" <?php echo $checkAB;?>> 2nd Choice</p>
<p><input type="radio" name="QA" value="AC" <?php echo $checkAC;?>> 3rd Choice</p>
[/HTML]

It is posting to the database OK, but when I bring up the form again, nothing is checked. I've even tried this:


[HTML]<input type="radio" name="QA" value="AA" <?php echo ($QA=='AA')? 'checked' : '' ?> >1st Choice
<input type="radio" name="QA" value="AB" <?php echo ($QA=='AB')? 'checked' : '' ?> >2nd Choice
<input type="radio" name="QA" value="AC" <?php echo ($QA=='AC')? 'checked' : '' ?> >3rd Choice[/HTML]

No luck. I worked on this all day. What am I doing wrong?
Nov 6 '07 #5

Atli
Expert 5K+
P: 5,058
Hi.

Thats strange. Given that the $QA variable contains a value that is either 'AA', 'AB' or 'AC', both of your code snippets should work.

I did a little test on this myself, worked perfectly:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $QA = $_POST['QA'];
  3. ?>
  4. <form action="?" method="POST">
  5.     <input type="radio" name="QA" value="AA" <?php if($QA=='AA')echo "checked"; ?> /> AA <br />
  6.     <input type="radio" name="QA" value="AB" <?php if($QA=='AB')echo "checked"; ?> /> AB <br />
  7.     <input type="radio" name="QA" value="AC" <?php if($QA=='AC')echo "checked"; ?> /> AC <br />
  8.     <input type="submit" />
  9. </form>
  10.  
Nov 6 '07 #6

code green
Expert 100+
P: 1,726
I think you are trying to create a sticky form.
You are close but need to read the $_POST variables.
I assume you are POSTing a form
ie
[PHP]<input type="radio" name="QA" value="AA" <?php if(isset($_POST['AA']))
echo 'checked'; ?> >1st Choice[/PHP]
Nov 6 '07 #7

P: 18
I finally got it to work using this:

[PHP]<input type="radio" name="QA" value="AA" <?php if($row[QA]=='AA')echo "checked"; ?> /> AA <br />[/PHP]
Since I'm pulling it from a certain row in my database I guess I needed the $row[].

Thanks for the help!
Nov 7 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.