PHP form arrays not received post - php

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.

Related

Radio button $_POST

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)

Multiple radio button array for php 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'];
?>

how to group radio buttons based on same class instead of same name

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">

How to handle multiple checkboxes in a PHP form?

I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.

how i can select multiple values from check box?

hi i have multiple option in check box and when visitor or customer select multiple option then how i can get multiple values? plz explain with code thanks
Name the checkboxes with [] (or PHP will drop all but one of them (I don't recall if it is the first or last)).
<input type=checkbox name="foo[]" value="some value">
Then they will be accessible as an array in the $_GET or $_POST superglobal.
$_GET['foo'][]
Basically, set all the name tags to be the same for all your checkboxes (with []). Then in your script, the values will be available as an array
Html:
<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
<input type="checkbox" name="tags[]" value="3" />
<input type="checkbox" name="tags[]" value="4" />
PHP:
print_r($_REQUEST['tags']);
Reference: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
Like this
<input type="checkbox" name="foo[]" value="bar" />
<input type="checkbox" name="foo[]" value="baz" />
<input type="checkbox" name="foo[]" value="qux" />
<?php
print_r($_POST['foo']);

Categories