Submitting multiple fields in a form (PHP) - 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 :
<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
)

Related

Selecting Correct Answer When Adding Questions To Database Via HTML Form

I have a form which has the question and 4 answers (it's a quiz), I tried to comment on https://stackoverflow.com/a/29280091/6482242 but I don't have enough reputation yet. This is very similar to what I have (as well as How to set the correct answer via checkbox? ).
The layout that I have is:
<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>
<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />
There is some PHP in the background to grab these and put them into a database, but I need to know please what the correct syntax is to link a radio button to a text input?
Thanks in advance :D
You should change the values of radio buttons and then store its value to a separate column in table with column something like correct_answer
<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>
<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="2">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="3">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="4">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />
And in that column just store the number of the selected Radio. So with each question, you will be storing 4 options, and among those 4 there will be one correct answer and its index will be stored in a separate table where you store you answer options.

How to rank (prioritize) values from numbered checkbox form in PHP

I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..

PHP var_dump only outputting value of bottom checkbox

Hi and thanks for reading my question. I am using a simple form to get some input :
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries" value="USA" /> USA<br />
<input type="checkbox" name="countries" value="Canada" /> Canada<br />
<input type="checkbox" name="countries" value="Japan" /> Japan<br />
<input type="checkbox" name="countries" value="China" /> China<br />
<input type="checkbox" name="countries" value="France" /> France<br />
<input type="submit" value="Order">
</form>
I want to make sure order.php is geting all of the choices selected, so order.php only contains the following code :
<pre>
<?php var_dump($_POST);?>
</pre>
Unfortunately, it is only outputting whatevre is the bottom-most checkbox that is checked.
The output is like this :
array(1) {
["countries"]=>
string(6) "Canada"
}
If i try the following code for output :
<?php
foreach($_POST as $key=>$post_data){
echo "You posted:" . $key . " = " . $post_data . "<br>";
}
?>
I get this output :
You posted:countries = Canada
Can anyone tell me where i am going wrong and how i can retrieve all of the data, for every box that is ticked ?
Thank you.
You gave the same name to your checkboxes, and PHP will overwrite previously parsed name submissions with the current value. You need to use the array-notation hack:
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
^^
which then makes $_POST['countries'] an array of all the values submitted.
echo "You posted: " . implode(',', $_POST['countries']);
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Order">
</form>
Change it to above, this will store all your checkboxes results for you!

How can i fetching,viewing and updating checkbox values stored in db

i have a html form that has a more then 30 checkboxes. checkboxes are used to select for different service.
I want to store those all value in 1 Database field seperated by , those could be numeric or string. Code for inserting checkbox values is as follows:
<form method="post" action="posted.php">
Option 1: <input type="checkbox" name="type" value="1" /><br />
Option 2: <input type="checkbox" name="type" value="2" /><br />
Option 3: <input type="checkbox" name="type" value="3" /><br />
Option 4: <input type="checkbox" name="type" value="4" /><br />
Option 5: <input type="checkbox" name="type" value="5" /><br />
Option 6: <input type="checkbox" name="type" value="6" /><br />
Option 7: <input type="checkbox" name="type" value="7" /><br />
Option 8: <input type="checkbox" name="type" value="8" /><br />
Option 9: <input type="checkbox" name="type" value="9"/><br />
<input type="submit" value="Submit" />
</form>
After successful submission off course i need to fetch those data in the corespondent check box while update.
Please response me as soon as possible with complete PHP code.
You can use JSON. From this:
Option 1: <input type="checkbox" name="myval[option_name1]" /><br />
Option N: <input type="checkbox" name="myval[option_nameN]" /><br />
Store:
$model->field = json_encode($_POST['myval']);
Or comma-seporated (if you really want):
$model->field = implode(",", $_POST['myval']);

Recording values of radio buttons in a db(php)

I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to do it?
My html code is as follows:
<li>Question1 </li>
<br /><form>A.
<input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
<label for="1ARadioButton">Answer1</label><br /><br />
B.
<input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
<label for="1BRadioButton"> Answer2</label>
</form><br /><br />
<li>Question2</li>
<br /><form>A.
<input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
<label for="2ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
<label for="2BRadioButton">Answer2</label>
</form><br /><br />
<li>Question3</li>
<br /><form>A.
<input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
<label for="3ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
<label for="3BRadioButton">Answer2</label>
</form><br /><br />
You've got the same name on all your radio buttons, so you're only going to get the value of the LAST radio button that's checked off. You're going to be recording the same answer for ALL questions (which is usually C, right?).
element IDs are NOT used for form submissions. They're used only for DOM operations. On HTML forms, only the name and value type attributes are relevant for the form submission process.
What you should have is:
Question 1:
<input type="radio" name="question1" value="option_A" />
<input type="radio" name="question1" value="option_B" />
Question 2:
<input type="radio" name="question2" value="option_A" />
<input type="radio" name="question2" value="option_B" />
etc...
As for storing them in the database, that's the same as storing any other form-data in a database. Get the radio's value with $_POST['question1'] or whatever, and do the usual escaping/query building/inserting.

Categories