I currently have a page which dynamically fills in a survey (Questions, Answers as Radio Buttons/Checkboxes) from a MySQL database. The generated HTML looks something like this :
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[0]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[0]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[0]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio[1]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio[1]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio[1]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio[1]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio[2]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio[2]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio[2]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio[3]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
</form>
A few important things to note :
- There are 3 types of questions, "Choice" - Single choice(Radio Button); "Multiple" - Multiple Choice(Check box); "Open" - User Input (Text Box).
- Each element's name corresponds to the appropriate question number (The number shown next to the question is Question+1 (Since it starts at 0). [For example, Question 14 would have Radio[14] as the name.
My Main Question : How can you submit these fields to be stored into the Database? I am trying to figure out how to write code which will find out which option is selected for each question.
Side Question : Is it also possible to validate these questions to ensure atleast one option is selected for each question? (Checking that textbox!="" is easy, but how would I do this for Radio Button/Checkboxes?)
PHP Code used to generate this form can be provided if needed! It is essentially using one variable to store the question number ($qno), which is used as a counter while looping the statements to pull data from MySQL, Figure out the type of answer, and place the appropriate controls on the form.
Option that is selected , will be in your $_POST
array and radio2
instead of radio[2]
even if yours works too, or use name radio[]
in all of your radio buttons ,you will get array that contains all radio buttons that are selected.
Also , options that are checked should be in an array that is in the same $_POST
array
You use a simple name for checkbox,this will only send the last value checked to your php script and will work as radio even if more than one value is checked so:
Instead of name="Check4"
it must be name="Check4[]"
.
And for displaying answers , you can iterate over values of $_POST
simply like this :
<?php
if($_POST['submit']) {
foreach($_POST as $key=>$value){
echo "Input name : $key Value:$value";//add condition to exclude your button or hidden fields
}
}
?>
Do something like this:
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" id="radio[0]" value="Alien" name="question1" />Alien
<br />
<input type="radio" id="radio[1]" value="Hobbit" name="question1" />Hobbit
<br />
<input type="radio" id="radio[2]" value="Tree" name="question1" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" id="radio[3]" value="Camel Collector" name="question2" />Camel Collector
<br />
<input type="radio" id="radio[4]" value="sadasd" name="question2" />sadasd
<br />
<input type="radio" id="radio[5]" value="Voolome" name="question2" />Voolome
<br />
<input type="radio" id="radio[6]" value="31231235" name="question2" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" id="radio[7]" value="Nobody Knows" name="question3" />Nobody Knows
<br />
<input type="radio" id="radio[8]" value="Somebody Knows" name="question3" />Somebody Knows
<br />
<input type="radio" id="radio[9]" value="Who Knows" name="question3" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" id="radio[10]" value="Answer1" name="question4" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" value="Bike" name="question5[]">Answer One<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Two<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="question6" />
<br /><br/>
</form>
to validate radio button use this:
if($("#radio:checked").length==0)
{
alert("Please Select atleast one");
return false;
}
take reference of this Building a Simple Quiz
can you please used this code :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio1[]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio1[]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio1[]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio1[]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio2[]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio2[]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio2[]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio3[]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
<input type="submit" name="submit">
</form>
</body>
</html>
PHP Code
<?php if($_POST['submit']) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;
}
?>
Output
Array
(
[radio] => Array
(
[0] => Hobbit
)
[radio1] => Array
(
[0] => sadasd
)
[radio2] => Array
(
[0] => Somebody Knows
)
[Ans5] =>
[submit] => Submit Query
)