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 andradio2
instead ofradio[2]
even if yours works too, or use nameradio[]
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
arrayYou 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 bename="Check4[]"
.And for displaying answers , you can iterate over values of
$_POST
simply like this :Do something like this:
to validate radio button use this:
take reference of this Building a Simple Quiz
can you please used this code :
PHP Code
Output