Submitting Multiple Fields In A Form (php)
Solution 1:
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 :
<?phpif($_POST['submit']) {
foreach($_POSTas$key=>$value){
echo"Input name : $key Value:$value";//add condition to exclude your button or hidden fields
}
}
?>
Solution 2:
Do something like this:
<formid="form1"name="form1"method="post"action="">
1 . How do you classify yourself?
<br/><inputtype="radio"id="radio[0]"value="Alien"name="question1" />Alien
<br /><inputtype="radio"id="radio[1]"value="Hobbit"name="question1" />Hobbit
<br /><inputtype="radio"id="radio[2]"value="Tree"name="question1" />Tree
<br /><br/>
2 . Who are you?
<br/><inputtype="radio"id="radio[3]"value="Camel Collector"name="question2" />Camel Collector
<br /><inputtype="radio"id="radio[4]"value="sadasd"name="question2" />sadasd
<br /><inputtype="radio"id="radio[5]"value="Voolome"name="question2" />Voolome
<br /><inputtype="radio"id="radio[6]"value="31231235"name="question2" />31231235
<br /><br/>
3 . Test Question
<br/><inputtype="radio"id="radio[7]"value="Nobody Knows"name="question3" />Nobody Knows
<br /><inputtype="radio"id="radio[8]"value="Somebody Knows"name="question3" />Somebody Knows
<br /><inputtype="radio"id="radio[9]"value="Who Knows"name="question3" />Who Knows
<br /><br/>
4 . Test Question 2
<br/><inputtype="radio"id="radio[10]"value="Answer1"name="question4" />Answer1
<br /><br/>
5 . First Multiple
<br/><inputtype="checkbox"value="Bike"name="question5[]">Answer One<br><br /><inputtype="checkbox"value="Bike"name="question5[]">Answer Two<br><br /><inputtype="checkbox"value="Bike"name="question5[]">Answer Three<br><br /><br/>
6 . First Open!
<br/><inputtype="text"name="question6" /><br /><br/></form>
Solution 3:
to validate radio button use this:
if($("#radio:checked").length==0)
{
alert("Please Select atleast one");
returnfalse;
}
Solution 4:
take reference of this Building a Simple Quiz
can you please used this code :
<!DOCTYPE html><html><head></head><body><formid="form1"name="form1"method="post"action="">
1 . How do you classify yourself?
<br/><inputtype="radio"name="radio[]"id="radio[0]"value="Alien" />Alien
<br /><inputtype="radio"name="radio[]"id="radio[1]"value="Hobbit" />Hobbit
<br /><inputtype="radio"name="radio[]"id="radio[2]"value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/><inputtype="radio"name="radio1[]"id="radio[3]"value="Camel Collector" />Camel Collector
<br /><inputtype="radio"name="radio1[]"id="radio[4]"value="sadasd" />sadasd
<br /><inputtype="radio"name="radio1[]"id="radio[5]"value="Voolome" />Voolome
<br /><inputtype="radio"name="radio1[]"id="radio[6]"value="31231235" />31231235
<br /><br/>
3 . Test Question
<br/><inputtype="radio"name="radio2[]"id="radio[7]"value="Nobody Knows" />Nobody Knows
<br /><inputtype="radio"name="radio2[]"id="radio[8]"value="Somebody Knows" />Somebody Knows
<br /><inputtype="radio"name="radio2[]"id="radio[9]"value="Who Knows" />Who Knows
<br /><br/>
4 . Test Question 2
<br/><inputtype="radio"name="radio3[]"id="radio[10]"value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/><inputtype="checkbox"name="Check4"value="Bike">Answer One<br><br /><inputtype="checkbox"name="Check4"value="Bike">Answer Two<br><br /><inputtype="checkbox"name="Check4"value="Bike">Answer Three<br><br /><br/>
6 . First Open!
<br/><inputtype="text"name="Ans5" /><br /><br/><inputtype="submit"name="submit"></form></body></html>
PHP Code
<?phpif($_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
)
Post a Comment for "Submitting Multiple Fields In A Form (php)"