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.
Related
I have the following 3 checkbox tags where each represents a category.
<input type="checkbox" name="catpref[]" value=1> Politics<br>
<input type="checkbox" name="catpref[]" value=2> Sports<br>
<input type="checkbox" name="catpref[]" value=3> Economics<br>
I need to add next to each category another checkbox that will return "1" if selected for the corresponding category.
<input type="checkbox" name="catpref[]" value=1> Politics
<input type="checkbox" name="worldwide" value=1>
<input type="checkbox" name="catpref[]" value=2> Sports
<input type="checkbox" name="worldwide" value=1>
<input type="checkbox" name="catpref[]" value=3> Economics
<input type="checkbox" name="worldwide" value=1>
If I submit the above, there is no link between each catpref[] and it's corresponding worldwide checkbox. How can this be done ?
Just do it the same as categories
<input type="checkbox" name="catpref[]" value="1"> Politics
<input type="checkbox" name="worldwide[]" value="1">
<input type="checkbox" name="catpref[]" value="2"> Sports
<input type="checkbox" name="worldwide[]" value="2">
<input type="checkbox" name="catpref[]" value="3"> Economics
<input type="checkbox" name="worldwide[]" value="3">
In your backend you will get a worldwide array with corresponding category value if selected, f.eg. if
$_POST['catpref'] = [1,2]
$_POST['worldwide'] = [2]
it means category 1 and 2 were selected with 2nd worldwide.
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);
I have text boxes with a default value. I want to put them in array output as shown below.
P.S: I need the expected array output (shown below) for my dynamic options in my online quiz program when creating questions
if(isset($_POST['btn_submit'])
{
//code here
}
<form method="post">
//question 1 array index [0]
<input type="text" name="option" value="1">
<input type="text" name="option" value="2">
<input type="text" name="option" value="3">
//question 2 array index [1]
<input type="text" name="option" value="1">
<input type="text" name="option" value="2">
<input type="submit" name="btn_submit">
</form>
EXPECTED ARRAY OUTPUT VALUES:
array (
[0] => 1,2,3
[1] => 1,2
)
EDIT:
It's possible to have the same name but still gets the expected array value? then, put them in one variable array
You need to use array in name attribute as:
<form method="post">
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">
<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">
</form>
PHP:
if(isset($_POST['btn_submit']))
{
$newArr[] = implode(',',$_POST['option_1']);
$newArr[] = implode(',',$_POST['option_2']);
echo "<pre>";
print_r($newArr);
}
Result:
Array
(
[0] => 1,2,3
[1] => 1,2
)
If you just want to use one single name option than use this:
<form method="post">
<input type="text" name="option[0][]" value="1">
<input type="text" name="option[0][]" value="2">
<input type="text" name="option[0][]" value="3">
<input type="text" name="option[1][]" value="1">
<input type="text" name="option[1][]" value="2">
<input type="submit" name="btn_submit">
</form>
PHP:
if(isset($_POST['btn_submit']))
{
$newArr[] = implode(',',$_POST['option'][0]);
$newArr[] = implode(',',$_POST['option'][1]);
echo "<pre>";
print_r($newArr);
}
Replace your code with following code and IT WILL WORK.
if(isset($_POST['btn_submit'])
{
//code here
}
<form method="post">
//question 1 array index [0]
<input type="text" name="option[0][]" value="1">
<input type="text" name="option[0][]" value="2">
<input type="text" name="option[0][]" value="3">
//question 2 array index [1]
<input type="text" name="option[1][]" value="1">
<input type="text" name="option[1][]" value="2">
<input type="submit" name="btn_submit">
</form>
You can simply use [] before the option name to make it as an array.
//question 1 array index [0]
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">
//question 2 array index [1]
<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">
Trying to create a search function using multiple checkboxes, but seem to be only finding information related to $_POST requests.
<input type="checkbox" name="fruit" value="1">
<input type="checkbox" name="fruit" value="2">
<input type="checkbox" name="fruit" value="3">
<input type="checkbox" name="fruit" value="4">
<input type="checkbox" name="fruit" value="5">
What is the best way to get the query to look like this
search?fruit=1,2,3,4
Is there a way to do this non ajax?
Just to clarify...
Each value represents a different fruit.
Originally I did this
search?apple=1&orange=1
As I added more checkboxes, the query seemed inefficient.
I know that I can add the checkboxes into the array using the $_POST method
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
As a few have suggested using this technique for a $_GET query would look something like this
search?fruit[]=1&fruit[]=2&fruit[]=5
So the question is really how to clean it up (comma seperated)
NOTE: As others have pointed out, it isn't necessary to pass a single parameter with a comma-separated value to end up with an array or a comma-separated value on the server. Frameworks like PHP can handle this with no JavaScript required. You can simply give each checkbox the same "name" attribute. That will cause multiple parameters with the same name to be passed to the server, which is just fine. In fact, it is the same result you would get if you used a <select multiptle="multiple"> element.
In PHP, if you use a name with square brackets at the end, like fruit[], you can then get an array on the server with:
$_GET['fruit']
And if you want a comma-separated value on the server, you can use:
implode(',', $_GET['fruit'])
But if you really want a single parameter with a comma-separated value, here is how you can do it:
You can use a form with a hidden input. Set the form's "method" to "get" and the hidden input's "name" to "fruit". Then add an event handler that sets the value of the hidden input to the comma-separated string when the form is submitted.
HTML:
<form id="fruitForm" action="search" method="get">
<input type="hidden" name="fruit" />
<label><input type="checkbox" value="1" />apple</label><br />
<label><input type="checkbox" value="2" />banana</label><br />
<label><input type="checkbox" value="3" />orange</label><br />
<label><input type="checkbox" value="4" />pineapple</label><br />
<label><input type="checkbox" value="5" />grapefruit</label><br />
<button type="submit">submit</button>
</form>
JQuery:
$('#fruitForm').submit(function() {
var fruits = $('#fruitForm').find('input:checkbox:checked').map(function() {
return $(this).val();
}).get().join(',');
$('#fruitForm').find('input[name=fruit]').val(fruits);
});
Note: The checkbox elements do not have "name" attributes, so they do not get included in the form submission.
jsfiddle
Use fruit[] as the input name
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">
You will get the following:
echo "<pre>", print_r($_GET["fruit"], true);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Try this, Use checkbox name as fruit[]. In php get the fruit array and converted as string using implode()
<?php
echo $fruits = implode(",",$_GET['fruit']); //1,2,3,4,5
?>
<form>
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">
<input type="submit">
</form>
I'm new to this. I'm studying web development and have to create a php response form for a questionnaire which then inputs into a database. I'm having trouble with the radio buttons. I can't create the right code that makes an array and displays the answers in the response form/page.
This is my code:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer[1]" value="6"> 5<input type="radio" name="answer[1]" value="5">
4<input type="radio" name="answer[1]" value="4"> 3<input type="radio" name="answer[1]" value="3">
2<input type="radio" name="answer[1]" value="2"> 1<input type="radio" name="answer[1]" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer[2]" value="6"> 5<input type="radio" name="answer[2]" value="5">
4<input type="radio" name="answer[2]" value="4"> 3<input type="radio" name="answer[2]" value="3">
2<input type="radio" name="answer[2]" value="2"> 1<input type="radio" name="answer[2]" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer[3]" value="6"> 5<input type="radio" name="answer[3]" value="5">
4<input type="radio" name="answer[3]" value="4"> 3<input type="radio" name="answer[3]" value="3">
2<input type="radio" name="answer[3]" value="2"> 1<input type="radio" name="answer[3]" value="1">
</p>
I know the answer can relate to the isset function but I don't know how to code it.
Could someone possibly teach or help me out here?
When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST) the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.
When I created your HTML and tested it with a var_dump and some random selections, the output was
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
Notice that there is an array within the $_POST['answer'] variable. So you should foreach over each element in that array to handle each respective value:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
If you need to work with the answer number that you defined in the POST array, you can foreach with a key:
foreach ($_POST['answer'] as $answerNum => $answer) {
// do stuff with $answerNum and $answer
}
You can, of course, access your answer by its number directly:
if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
// do stuff with $_POST['answer'][1]
}
I don't imagine that this is quite what you are looking to do. If instead you give the three questions different names:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer1" value="6"> 5<input type="radio" name="answer1" value="5">
4<input type="radio" name="answer1" value="4"> 3<input type="radio" name="answer1" value="3">
2<input type="radio" name="answer1" value="2"> 1<input type="radio" name="answer1" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer2" value="6"> 5<input type="radio" name="answer2" value="5">
4<input type="radio" name="answer2" value="4"> 3<input type="radio" name="answer2" value="3">
2<input type="radio" name="answer2" value="2"> 1<input type="radio" name="answer2" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer3" value="6"> 5<input type="radio" name="answer3" value="5">
4<input type="radio" name="answer3" value="4"> 3<input type="radio" name="answer3" value="3">
2<input type="radio" name="answer3" value="2"> 1<input type="radio" name="answer3" value="1">
</p>
then in php all you will need to do is find the values using the $_POST variable as such
<?php
echo $_POST['answer1'];
echo $_POST['answer2'];
echo $_POST['answer3'];
?>