Post radio button value in array to php file - php

I'm trying to make a question paper. My code is like this:
<form action="check.php">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques12[]" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques12[]" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques12[]" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques12[]" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques13[]" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques13[]" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques13[]" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques13[]" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques14[]" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques14[]" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques14[]" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques14[]" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
The question is dynamic and above comes from a php and mysql code. I want to post the data to a php file where I can calculate the number of questions correct, number of questions wrong and number of questions attempted.
I'm confused how should I check the correct answers. $_POST[ans_a] would return ans_a from all questions. How can I distinguish all the questions and separately put them into array. Or should I change my way of arranging my dynamic code ?

Note what #RajdeepPaul said. Your answers are being exposed and posted along with the answers, so if you want any kind of security don't do this, and check for the answers in PHP..
$answers = ['ques12' => 4, 'ques13' => 3, 'ques14' => 2];
Now loop over all the answers. The keys in $answers are the same as in $_POST.

First of all, never put the correct answer as hidden input field in the HTML form. Anybody can see the source code and get the correct option for all the questions. Keep the correct answers(options) corresponding to particular questions in a table like this:
+-------------+----------------+
| question_id | correct_option |
+-------------+----------------+
| | |
Or, use an array like this:
$correct_options = array('ques_id_12' => 4, 'ques_id_13' => 3, 'ques_id_14' => 2);
So that later you could compare the correct answer with the user inputted option value.
Now comes to your question, you need to change the name attribute value of your checkbox elements like this,
name="ques_id_12" for all the checkbox options for question ID 12, name="ques_id_13" for all the checkbox options for question ID 13 etc.
Also, since you're sending bulk of data to the server with your form, you should use POST method instead of GET
So your form should be like this:
<form action="check.php" method="POST">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques_id_12" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques_id_12" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques_id_12" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques_id_12" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques_id_13" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques_id_13" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques_id_13" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques_id_13" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques_id_14" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques_id_14" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques_id_14" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques_id_14" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
And after form submission, you can use simple foreach loop to compare the user inputted option value with the correct option value for each question like this:
foreach($_POST as $question_id => $user_inputted_option){
// compare the user inputted option value with the correct option value
}
Update(1):
How I would know which questions have been answered and which have not ? And how would I pass the question id's ?
Let's say your question id array is like this:
$ques_ids = array('ques_id_12', 'ques_id_13', 'ques_id_14', 'ques_id_15', 'ques_id_16');
Then after form submission, you need to process your form like this:
$array_keys = array_keys($_POST); // all the user attempted question ids
foreach($ques_ids as $q_id){
if(in_array($q_id, $array_keys)){
// attempted question
$user_inputted_option_value = $_POST[$q_id];
// now compare the user inputted option value with the correct option value
}else{
// unattempted question
}
}
Sidenote: If you want to see the complete array structure, do var_dump($_POST);

Related

Inserting multiple rows from multiple question radio form in PHP

I have a form to determine the user's personality. So each question (there are 25) has 4 possible answers to choose from:
<form method="post">
<div>
<label for="name"></label>
<input name="name" id="name" placeholder="Name">
</div>
<div>
<label for="surname"></label>
<input name="surname" id="surname" placeholder="Surname">
</div>
<br>
<div>
<label for="q1">Question 1</label><br>
<input type="radio" name="q1" id="q1" value="S"> q1c1 <br>
<input type="radio" name="q1" id="q1" value="D"> q1c2 <br>
<input type="radio" name="q1" id="q1" value="K"> q1c3 <br>
<input type="radio" name="q1" id="q1" value="I"> q1c4 <br>
</div>
<br>
<div>
<label for="q2">Question 2</label><br>
<input type="radio" name="q2" id="q2" value="D"> q2c1 <br>
<input type="radio" name="q2" id="q2" value="K"> q2c2 <br>
<input type="radio" name="q2" id="q2" value="I"> q2c3 <br>
<input type="radio" name="q2" id="q2" value="S"> q2c4 <br>
</div>
<br>
<div>
<label for="q3">Question 3</label><br>
<input type="radio" name="q3" id="q3" value="S"> q3c1 <br>
<input type="radio" name="q3" id="q3" value="I"> q3c2 <br>
<input type="radio" name="q3" id="q3" value="D"> q3c3 <br>
<input type="radio" name="q3" id="q3" value="K"> q3c4 <br>
</div>
<br><br>
<button>Submit</button>
</form>
I need to insert the name, surname, and the selection of the question in one line. The next row in the database should also contain the name, surname, and then the next question's selection for all 25 questions.
My table users in the database only has 3 columns namely: name, surname, q
I am very new to PHP and tried some previous questions but none worked for my particular scenario.
I assume you are working fully with PHP, so no JavaScript or Ajax involved.
first of all you need to specify where your request should send:
<form method="post" action="https://example.org/target.php">
in target.php, you need to read data out of $_POST. In your case, it must be something like $_POST['name'].
From there on, you can do whatever you want with the data.
Please consider this answer as a minimal question as you need to do further checks, such as security checks, prevent SQL injections and overall data validation.

i have multiple radio button in same name as array and same value [duplicate]

This question already has an answer here:
I have Multiple radio button with same name in it of array and have same values as 1
(1 answer)
Closed 4 years ago.
i have multiple radio button in same name as array and same value for all.
<input type="radio" name="radio_name[]" id="radi_name" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
in php i used like this
$a[]=$_post['radio_name'];
prinr_r($a);
im getting result like this :
Array ( [0] => 1 )
if i uncheck the button set as zero i want result to be like this
Array ( [0] => 1,[1] => 0,[2] => 0,[3] => 0 )
Please check this images i have form like this
You need to use only [] instead of array keys also use checkbox instead of radio . So use following code it will work for you.
<input type="checkbox" name="radio_name[]" id="radi_name1" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name2" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name3" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name4" value="1" >
<label for="radio1">Set as Default</label>
I am pretty sure that radio buttons don't get sent to the server if none of the values is selected. In your html you really have 4 different radio button groups so only the ones that have a selected value, get sent.
If you want 4 groups where a value always gets sent for each group, you should do something like this:
<input type="radio" name="radio_name[1]" value="1" id="radio1" checked>
<input type="radio" name="radio_name[1]" value="0">
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[2]" value="1" id="radio2">
<input type="radio" name="radio_name[2]" value="0" checked>
<label for="radio2">Set as Default</label>
// etc.
This way you will have independent radio button groups and you will get the result you want.
You have to use checkbox instead of radio. Radio only allowed 1 value being posted, and remove the array key like dipmala suggested.

count html radio buttons value with php

I have a html form with grouped radio button. This form contains questions with yes or no options
<form action="results.php" method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
I know i can count the number of radio buttons with name q
$count_cbox = count($_POST['q'])
But is it possible that when the user makes a choice i count the radio button value that ="yes" or "no".
Please change your radio button names slightly. Otherwise the grouping doesn't work:
<form method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[0]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[0]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[1]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[1]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[2]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[2]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
The PHP code:
for($i = 0; $i < count($_POST['q']); ++$i) {
if($_POST['q'][$i] == 'yes') {
++$yes;
}
}
Now $yes contains the number of radio buttons with the value yes. In this case 0-3. Please pay attention to the fact that if no radio button is selected, it will return NULL, not 0.

Multiple Choices PHP

I made a multiple choice with PHP (Codeigniter) and MySQL. I Got trouble when trying to retrieve value from the answer (with dynamic name) for each question. Here is the code for radio button :
<input type="radio" name="question_id (according to id of question)" value="answer_id">
So, if I have 3 random questions, the structure will be
<p>Question number 1 goes here</p>
<input type="radio" name="question_id1[]" value="1">
<input type="radio" name="question_id1[]" value="2">
<input type="radio" name="question_id1[]" value="3">
<input type="radio" name="question_id1[]" value="4">
<input type="radio" name="question_id1[]" value="5">
<p>Question number 6 goes here</p>
<input type="radio" name="question_id6[]" value="1">
<input type="radio" name="question_id6[]" value="2">
<input type="radio" name="question_id6[]" value="3">
<input type="radio" name="question_id6[]" value="4">
<input type="radio" name="question_id6[]" value="5">
<p>Question number 9 goes here</p>
<input type="radio" name="question_id9[]" value="1">
<input type="radio" name="question_id9[]" value="2">
<input type="radio" name="question_id9[]" value="3">
<input type="radio" name="question_id9[]" value="4">
<input type="radio" name="question_id9[]" value="5">
How to retrieve the answer that relates to the question ? For example put it in Array like :
array p = ['id_question' => 21, 'id_answer'=4]
The radio button structure
Try with
<p>Question number 9 goes here</p>
<input type="radio" name="question_id[9]" value="1">
<input type="radio" name="question_id[9]" value="2">
<input type="radio" name="question_id[9]" value="3">
<input type="radio" name="question_id[9]" value="4">
<input type="radio" name="question_id[9]" value="5">
<p>Question number 6 goes here</p>
<input type="radio" name="question_id[6]" value="1">
<input type="radio" name="question_id[6]" value="2">
<input type="radio" name="question_id[6]" value="3">
<input type="radio" name="question_id[6]" value="4">
<input type="radio" name="question_id[6]" value="5">
And in server side
<?php
//assuming form method is post
$questions_array = array();
foreach($_POST[question_id] as $key=>$answer)
{
$questions_array[] = array('id_question' => $key, 'id_answer'= $answer);
}
print_r($questions_array);
?>
Assuming that user can select a single radio button for single question....
I have placed the code in the form and with a submit button i submitted it. Then the output i got it is
Array
(
[question_id1] => Array
(
[0] => 3
)
[question_id6] => Array
(
[0] => 2
)
[question_id9] => Array
(
[0] => 3
)
[submit] => submit
)
So we can access with the same name. I did it with normal php code. Because the array will that is formed will be same in both core php as well as codeigniter. So we can normally access it with for loop.

Selecting multiple radio buttons (through jquery, javasscript?)

I currently have a form with lets say 5 radio button entries (see below).
What im looking archive is the following:
- Being able to choose multiple radio buttons - lets say 3 and submit the form.
Currently I got it working fine with PHP, SQL, but im only able to choose one radiobutton and submit that.
I figure it would also come in handy being able to deselect a radio button in case you wrongly click one.
My guess is that this can be done through some javascript? Any suggestions? Online examples perhaps?
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="radio" value="1" name="poll">
<label for="option-1">Select option 1</label>
<input id="option-2" type="radio" value="2" name="poll">
<label for="option-2">Select option 2</label>
<input id="option-3" type="radio" value="3" name="poll">
<label for="option-3">Select option 3</label>
<input id="option-4" type="radio" value="4" name="poll">
<label for="option-4">Select option 4</label>
<input id="option-5" type="radio" value="5" name="poll">
<label for="option-5">Select option 5</label>
</form>
Radio buttons are designed so that only one option from each group (as designated by their shared name) can be selected at once (just like you can only tune a radio to one station).
The input control which allows any number of options to be selected is the checkbox. If you append [] to their names, then the selected options will arrive on the PHP side as an array.
<input type="checkbox" value="1" name="poll[]" />
<input type="checkbox" value="2" name="poll[]" />
As it has same name poll you will not be able to do that as input type radio is specialized in selecting a single value from multiple inputs.
You can use input type checkbox for that and make them as an array:
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="checkbox" value="1" name="poll[]">
<label for="option-1">Select option 1</label>
<input id="option-2" type="checkbox" value="2" name="poll[]">
<label for="option-2">Select option 2</label>
<input id="option-3" type="checkbox" value="3" name="poll[]">
<label for="option-3">Select option 3</label>
<input id="option-4" type="checkbox" value="4" name="poll[]">
<label for="option-4">Select option 4</label>
<input id="option-5" type="checkbox" value="5" name="poll[]">
<label for="option-5">Select option 5</label>
</form>
LIMIT (with jQuery) the number:
$("input[type=checkbox][name=poll[]]").click(function() {
var numberSel = $("input[type=checkbox][name=poll[]]:checked").length >= 3;
$("input[type=checkbox][name=poll[]]").not(":checked").attr("disabled",numberSel);
});
Radio buttons are designed for only 1 item to be selected, use checkboxes instead.

Categories