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'];
?>
Related
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.
I've been playing with trying to get the first 4 of a checkbox array to act like radio buttons for a while now and I thought I had it but I can't figure it out.. I searched through here and found..
Checkbox to act like radio button, check first four or last four checkboxes but not both- javascript?
Though I've haven't quite been able to make it work in my case apparently. So to re-iterate. I need the first 4 you can only pick 1, while the others can be multiple of any combination. I know its something small I have to be missing.
HTML
<form name="account_info" id="account_info">
<input type="checkbox" name="permissions[]" value="0" onClick="checkOnly(this)"> Permission 0<br>
<input type="checkbox" name="permissions[]" value="1" onClick="checkOnly(this)"> Permission 1<br>
<input type="checkbox" name="permissions[]" value="2" onClick="checkOnly(this)"> Permission 2<br>
<input type="checkbox" name="permissions[]" value="3" onClick="checkOnly(this)"> Permission 3<br>
<input type="checkbox" name="permissions[]" value="4"> Permission 4<br>
<input type="checkbox" name="permissions[]" value="5"> Permission 5<br>
<input type="checkbox" name="permissions[]" value="6"> Permission 6<br>
<input type="checkbox" name="permissions[]" value="7"> Permission 7<br>
</form>
The Script
function checkOnly(myCheckbox) {
for(var i = 0; i < 4; i++) {
if(document.account_info.elements[i].name != myCheckbox.name) {
document.account_info.elements[i].checked = false;
}
}
}
Here is the link to jsfiddle
http://jsfiddle.net/np4dry0f/3/
The problem is that the name attribute will always be permissions[] in javascript.
A possible solution is to use the value instead of the name:
function checkOnly(myCheckbox) {
for(var i = 0; i < 4; i++) {
if(document.account_info.elements[i].value != myCheckbox.value) {
document.account_info.elements[i].checked = false;
}
}
}
See an example on JS Bin.
Much easier than looping through each checkbox
$('.radio').change(function() {
$('.radio').not(this).removeAttr('checked');
$(this).attr('checked','checked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form name="account_info" id="account_info">
<input class="radio" type="checkbox" name="permissions[]" value="0"> Permission 0<br>
<input class="radio" type="checkbox" name="permissions[]" value="1"> Permission 1<br>
<input class="radio" type="checkbox" name="permissions[]" value="2"> Permission 2<br>
<input class="radio" type="checkbox" name="permissions[]" value="3"> Permission 3<br>
<input class="radio" type="checkbox" name="permissions[]" value="4"> Permission 4<br>
<input class="radio" type="checkbox" name="permissions[]" value="5"> Permission 5<br>
<input class="radio" type="checkbox" name="permissions[]" value="6"> Permission 6<br>
<input class="radio" type="checkbox" name="permissions[]" value="7"> Permission 7<br>
</form>
I have a form, but I can not receive the data correctly.
I want to receive the id and the option you chose, how can I do this?
I tried to foreach, but could not.
<input type="hidden" id="Id_field[]" name="Id_field[]" value="1"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
<input type="hidden" id="Id_field[]" name="Id_field[]" value="2"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
Receiving correctly:
Id_field = 1
option = yes
Id_field = 2
option = no
First of all, id's need to be unique, so this isn't valid (but that doesn't matter for the question). What you can do is use the id in the name like this
<input type="checkbox" name="option[1]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[1]" class="validate[required]" value="no" />No
<input type="checkbox" name="option[2]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[2]" class="validate[required]" value="no" />No
Now you will know by the value if yes or no has been clicked and by the key the id. Also you might consider changing to radiobuttons, then one has to chose either one but can't answer both.
How to know the value of clicked button
<input type="radio" name="radio">Yes<br>
<input type="radio" name="radio">No
php part
$option1= $_POST['radio'];
When i echo it it always say's "ON".
You need to give a value attribute:
<input type="radio" name="radio" value="yes">Yes<br>
<input type="radio" name="radio" value="no">No
Try this:
<input type="radio" name="radio" value="Yes_value">Yes<br>
<input type="radio" name="radio" value="No_value">No
and in PHP
$option1 = $_POST['radio']; // "Yes_value" or "No_value"
Try This:
Write its value in input
<input type="radio" name="radio" value="Yes" >Yes<br>
<input type="radio" name="radio" value="No">No
Use value attribute
<input type="radio" name="radio" value="Yes" />
<input type="radio" name="radio" value="No" />
assigned different value on the radio button.
<input type="radio" name="radio" value="Yes">Yes<br>
<input type="radio" name="radio" value="No">No
<form>
<input type="radio" name="radio" value="Yes">Yes
<input type="radio" name="radio" value="No">No <br>
<input type="radio" name="radio2" value="Yes">Yes
<input type="radio" name="radio2" value="No">No <br>
<input type="radio" name="radio3">No Value
<input type="submit" value="submit">
</form>
<?php
print_r($_GET);
I suggest you put this in a new file and just run it, it will help you get a deeper understanding of how the radio button work in forms.
Play around, see the difference that u get with different names, and different values.
As you can see, a radio element with no value can only be on or blank (blank wont even be set in PHP)
Is there a way to group the set of radio buttons that share the same class.
I want to be able to be able to check the radio button from each set.
Currently it allows me to check only one radio button (as I know because of same name)
Is there a JQuery way either ?
Example:
SET A
<input type="radio" name="item[]" class="a" value="1"><br>
<input type="radio" name="item[]" class="a" value="2"><br>
<input type="radio" name="item[]" class="a" value="3"><br>
<input type="radio" name="item[]" class="a" value="4"><br>
SET B
<input type="radio" name="item[]" class="b" value="5"><br>
<input type="radio" name="item[]" class="b" value="6"><br>
<input type="radio" name="item[]" class="b" value="7"><br>
<input type="radio" name="item[]" class="b" value="8"><br>
Ok so you are trying to have multiple array entries, but using 1 array.
Since html interprets them as having the same name, you will have to add the numbers into the array key yourself.
So something like this should work.
<input type="radio" name="item[0]" class="a" value="1" />
<input type="radio" name="item[0]" class="a" value="2" />
<input type="radio" name="item[0]" class="a" value="3" />
<input type="radio" name="item[0]" class="a" value="4" />
<input type="radio" name="item[0]" class="a" value="5" />
<input type="radio" name="item[1]" class="b" value="6" />
<input type="radio" name="item[1]" class="b" value="7" />
<input type="radio" name="item[1]" class="b" value="8" />
<input type="radio" name="item[1]" class="b" value="9" />
<input type="radio" name="item[1]" class="b" value="10" />
$('#container').on('click', 'input[type="radio"]', function(){
this.name = this.className;
});
This should solves it.
Live DEMO
Can you not name the sets differently?
SET A
< input type="radio" name="item1[]" class="a" value="1">
< input type="radio" name="item1[]" class="a" value="2">
< input type="radio" name="item1[]" class="a" value="3">
< input type="radio" name="item1[]" class="a" value="4">
SET B
< input type="radio" name="item2[]" class="b" value="5">
< input type="radio" name="item2[]" class="b" value="6">
< input type="radio" name="item2[]" class="b" value="7">
< input type="radio" name="item2[]" class="b" value="8">