Skip to content Skip to sidebar Skip to footer

Submitting Multiple Fields In A Form (PHP)

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 :

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 :

<?php

 if($_POST['submit']) {

    foreach($_POST as $key=>$value){  
      echo "Input name : $key Value:$value";//add condition to exclude your button or hidden fields
    }
  }
 ?>

Solution 2:

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>

Solution 3:

to validate radio button use this:

if($("#radio:checked").length==0)
{
  alert("Please Select atleast one");
  return false;
}

Solution 4:

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
)

Post a Comment for "Submitting Multiple Fields In A Form (PHP)"