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("UP DATE XXX SET AA='$AA', AB='$AB', AC='$AC', BA='$BA', BB='$BB', BC='$BC' WHERE passcode='{$_SE SSION['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.
7 3632 Atli 5,058
Recognized Expert Expert
Hi.
I think you are misunderstandin g 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=t rue" to the <input> tag.
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?
Atli 5,058
Recognized Expert Expert
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; ?>
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?
Atli 5,058
Recognized Expert Expert
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: -
<?php
-
$QA = $_POST['QA'];
-
?>
-
<form action="?" method="POST">
-
<input type="radio" name="QA" value="AA" <?php if($QA=='AA')echo "checked"; ?> /> AA <br />
-
<input type="radio" name="QA" value="AB" <?php if($QA=='AB')echo "checked"; ?> /> AB <br />
-
<input type="radio" name="QA" value="AC" <?php if($QA=='AC')echo "checked"; ?> /> AC <br />
-
<input type="submit" />
-
</form>
-
code green 1,726
Recognized Expert Top Contributor
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]
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Craig Keightley |
last post by:
I have a page that has n number of radio groups (yes/No)
how can i prevent the form being submitted if more than one radio group is
not selected?
By default all radio groups are unchecked
The problem i am facing is that i do not know how many yes/no radio groups
will be generated
|
by: TS |
last post by:
My objective:
To create a survey that asks people to rate a list of things in order
of importance. I wish to prevent them from just rating every item in
the list the same value.
For example the Page would look roughly like this:
Please rate the following in order of importance:
Item Rating
Apples 1 2 3
Orange 1 2 3
|
by: Jay |
last post by:
I noticed somewhere on the net that a post mentioned that there was a bug in
the datagrid, radio buttons and a repeater. Something about the radio
buttons are not mutually exclusive. (will this be fixed soon)
I am trying to create a survey form where the 4 radio buttons are created
dynamically for each question in the database. Is there an example of this
somewhere. The radio buttons are based on a seperate table called survey
answers....
|
by: Brian |
last post by:
I have a list of plain HTML radio buttons that I want to be able to
loop through, get the values from them and insert them into a db. each
one should be a separate record... Can anyone please give me some kind
of example on how to do this???? I am doing this in asp.net VB.
Please let me know if you need any additional information.
Thanks
|
by: Jerry |
last post by:
We have a 10-question quiz for kids, each question being a yes or no
answer using radio selections. I'd like to keep a current total of
yes's and no's at the bottom of the quiz (if the user selects yes to
question 1, the total of yes's increases by 1). I've been searching for
a while but I'm not sure I'm searching with the right keywords. Can
anyone direct me to a source I can review to learn how to do this?
Thanks!
--
| |
by: IchBin |
last post by:
I am trying to set the state of a radio button. I do not see what I am
doing wrong. Sorry, I am new at this.. I need another set of eyes to
look at this snip of code. I am trying to set the radio button with this
link of code:
echo 'SCRIPT language=JavaScript
setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //?
<snip of code>
|
by: shror |
last post by:
i want to add a midi file to my pages and i want to put two radio
buttons so that on is on and the other is off so that the users can
start or stop the file playing by choosing one of the radio buttons,
but the problem is that i dont know how to do it.
does any body know how this could be done
Thanks in advance for your help
shror
www.s7els7.com
|
by: Franky |
last post by:
I am trying to create a form to be used for testing during a tutorial (at the end of each module the user will be given a test).
It contains a number of questions, each with a question and 4 possible answers. The answers are radio buttons, so the user can only select 1 for each question.
I am currently selecting 5 questions randomly from a database and displaying them using a loop:
$a = 1;
while ($row = mysql_fetch_assoc($result)){...
|
by: dpazza |
last post by:
Hi,
I'm creating a quiz on using a form in VB 2005 express.
I have four sets of questions and answers (labels and radio buttons)
and I change between which set of questions is currently shown on the
form by changing the visible state of the radio buttons and labels
utilising back and next buttons.
E.g. Next button makes current radio buttons and labels invisible and
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |