How can I use several form inputs with the same name? - php

I have several checkbox (I don't know number of them) that create from a loop in a form.
<form>
<input type="checkbox" name="id" value="id">
<input type="checkbox" name="id" value="id">
...//create in a loop
<input type="checkbox" name="id" value="id">
</form>
My question is that How can I read them, If I use <?php $_REQUEST['id']; ?>, it only reads the last checkbox.

Use an input array:
<input type="checkbox" name="id[]" value="id_a">
<input type="checkbox" name="id[]" value="id_b">
<input type="checkbox" name="id[]" value="id_c">
<!-- ^^ this makes it an array -->
$_REQUEST['id'] can be accessed:
foreach($_REQUEST['id'] as $id)
{
echo $id;
}
Outputs
id_a
id_b
id_c
Side note: this works with $_POST and $_GET (not just $_REQUEST). Generally speaking though $_REQUEST should be avoided if possible.

Use unique id's for your checkboxes, e.g.,
<form>
<input type="checkbox" name="id1" value="value1">
<input type="checkbox" name="id2" value="value2">
...//create in a loop
<input type="checkbox" name="id3" value="value3">
</form>

Related

How to add each of checked checkboxes to a signle row in MySQL

I have several checkboxes that contain names (ids as referrence to database) - see code below. How can I select all checked values and add them to database (via MySQL) each row for each checked?
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
After clicking "send" with requiered checked names, the result in database should look like this (I selected John and Mike):
Id
1
3
(only selected ones)
How can I achieve that?
You need to wrap your inputs in a <form> element and give this form a method of post:
<form method="post">
<input type="checkbox" value="1" name="names[]" />John
<input type="checkbox" value="2" name="names[]" />Peter
<input type="checkbox" value="3" name="names[]" />Mike
<input type="checkbox" value="4" name="names[]" />Kattie
<input type="submit" value="Send" name="send" />
</form>
This will allow you to post submitted data from your form inputs to your PHP.
Note: If your HTML is in a different file (ie not in the same file as your form) you can add the action attribute to your form (eg: action="fileWithPHP.php")
Now you can access all checked checkboxes in your PHP using $_POST['names']. This will allow you to get your array of checked values. You can then use a foreach loop to loop through every value in your array:
<?php
if(isset($_POST['names'])) {
$names = $_POST['names'];
foreach($names as $name) {
echo "Add " . $name . " to db here<br />"; // add $name to db
}
}
?>
You can wrap the inputs around a <form> and send it to php and retrieve using $_GET or $_POST and update the database.
I have used POST method here.
HTML:
<form action="test.php" method="post">
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
</form>
PHP:
if(!empty($_POST['names'])) {
foreach($_POST['names'] as $check) {
echo $check; //instead of echo you can insert it to the database
}
}

Submitting a form with every field the same name

Okay, so I learned that if I have a form like:
<form method="post" action="arrayplay2.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
that the attribute name="todelete[]" initiates an array. How? And then how do I access this and the values in each one with the $_POST superglobal on my arrayplay2.php script?
With form like that you get indeed an array in $_POST superglobal named todelete.
Array will be numeric array starting with index 0.
You can loop that array:
foreach($_POST['todelete'] as $val){
echo $val;
}
Or you can directly access desired index:
echo $_POST['todelete'][2];

Add hidden input types based on radio button selected

Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.

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

getting multiple checkboxes names/id's with php

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

Categories