PHP var_dump only outputting value of bottom checkbox - php

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!

Related

How to get multiple checked checkbox ID and VALUE at the same time

How to get by POST multiple checked checkbox IDs and VALUEs at the same time? Code bellow shows html to send data.
<form action="" method="post">
first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" />
second<input type="checkbox" name="first[]" id="second'<?php echo $data2; ?>'" value="second" />
third<input type="checkbox" name="first[]" id="third'<?php echo $data3; ?>'" value="third" />
<input type="submit" value="submit">
</form>
After send by post I get values, but ID is missing.
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value.'<br/>';
}
How can I send ID and VALUE and get them by post without explode them? For sure i can split them after, but there should be another way.
You could do something like:
<form action="" method="post">
first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
<input type="hidden" name="first[0][id]" value="first[]">
second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
<input type="hidden" name="first[1][id]" value="second[]">
third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
<input type="hidden" name="first[2][id]" value="third[]">
<input type="submit" value="submit">
</form>
And on the back-end:
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value['value'].'<br/>';
echo 'ID: '.$value['id'].'<br/>';
}
If you want to get an id value from an input, used the id as key in your name array
<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>
or
<input type="checkbox" name="first[1]" .../>
<input type="checkbox" name="first[2]" .../>
<input type="checkbox" name="first[3]" .../>
then when you loop over your posted inputs, include the key in the key=>value
foreach($_POST['first'] as $id => $value){
echo 'ID: '.$id.' => VALUE: '.$value.'<br/>';
}

Get variable on Wordpress to PHP

I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />

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']);

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

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.

Categories