getting multiple checkboxes names/id's with php
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().
I want to get id of multiple checkboxes
You missed one thing:
<input type="checkbox" name="check_list[]" value="" />
The check_list array should not have an empty value. You should have to put a value into it.
<?php
$query="select item_id, item from item";
$result=mysql_query("$query");
while($row=mysql_fetch_array($result)):;
?>
<input type="checkbox" name="check_list[]" value="<?php echo $row['item_id']; ?>" /><br>
<?php endwhile;?>
PHP: getting multiple checkboxes with different names/id's with php
The problem is that all your alternative fields for a given question have the same name, so only the last one is getting passed to the next page. To use array notation properly, name all your fields for question 1 <input type="text" name="alternative[1][]">
. Then in your $_POST
array on the next page, alternative
will be an array where element 1
is an array containing each of the options you submitted for question 1.
Then you'll iterate over $_POST['alternative'] with something like this:
foreach($_POST['alternative'] as $qid => $question) {
foreach($question as $altnum => $alternative) {
// deal with alternatives here
// using $qid for question ID and $altnum for alternative number
}
}
Get $_POST from multiple checkboxes
Set the name in the form to check_list[]
and you will be able to access all the checkboxes as an array($_POST['check_list'][]
).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
POST values from multiple checkboxes to SQL
The name attribute of the checkboxes are not set. PHP can't get any info about the checked checkboxes (unless you should use Jquery en do some other stuff). It's important to know that only checked checkboxes will be visible in the POST data.
Give your checkboxes a name, for example:
echo '<input class="btn" type="checkbox" name="team_checkbox_'.$row['id'].'"> ';
Then in the script created.php were your data is posted to you can catch these values by running the same SQL-query and trying
if(isset($_POST['team_checkbox_'.$row['id']])){
// $row['id'] is checked
}
Or just check all POST data:
$t = array();
foreach($_POST as $post_param_name => $value){
if(substr($post_param_name, 0, 14) == 'team_checkbox_'){
// substr($post_param_name, 14) is the ID of one of the checked groups
$t[] = substr($post_param_name, 14);
}
}
Now $t is an array containing the team ID's of the checkboxes. So you can save in your SQL: T1 = $t[1], ...
HTML multiple checkboxes with identical name= into PHP $_POST
It is possible to retrieve all variables when you have multiple elements with identical 'name' parameters.
After much scouring the internet for a solution, I wrote the following php script which grabs the raw post data, and parses it:
<?php
function RawPostToArray() {
$rawPostData = file_get_contents("php://input");
if($rawPostData) {
$rawPostData = explode('&', $rawPostData);
if($rawPostData && is_array($rawPostData) && count($rawPostData)) {
$result = array();
foreach($rawPostData as $entry) {
$thisArray = array();
$split = explode('=', urldecode($entry), 2);
$value = (count($split) == 2) ? $split[1] : '';
if(array_key_exists($split[0], $result)) {
if(is_array($result[$split[0]])) {
$result[$split[0]][] = $value;
}
else {
$thisArray[] = $result[$split[0]];
$thisArray[] = $value;
$result[$split[0]] = $thisArray;
}
}
else {
$result[$split[0]] = $split[1];
}
}
return $result;
}
else return array();
}
else return array();
}
?>
Any duplicate 'names' are bumped down into an array within the result, much the way $_POST does, when the HTML is coded correctly.
There is probably plenty of room for improvement here, and I'm sure not all situations are accounted for, but it works well for my application.
Suggestions are appreciated where improvements can be made.
Get all checkbox id on form submit php
You do not need to post ids here:
here
1) id contains room_list <?php echo $val;?>:
2) name contains room_list[]
3) Value contains <?php echo $val; ?>
Now you id is consist of room_list and $val
.$val
you are already getting in post as a value
So in $POST data you can dynamically generate id of all checkbox
ex.
foreach($_POST['room_list'] as $rooms) {
$id = 'room_list'+$rooms; // here $rooms contains the `$val` values
}
Create hidden input fields to get all checkbox values:
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="hidden" name="room_list_hidden[]" value="<?php echo $val; ?>" />
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
So in $_POST['room_list_hidden']
you will get all ids
Related Topics
PHP - Add Link to a Url in a String
Get the Hierarchy of a Directory with PHP
How to Select Default Value of a Field
Concatenate Values of N Arrays in PHP
Using Fetch_Assoc on Prepared Statements
What Does the MySQLi_Error() Expects Parameter 1 to Be MySQLi, Null Given Mean
Sort Multidimensional Array by Date Column, Then Use Other Column Values If Dates Are the Same
Mod_Rewrite, PHP and the .Htaccess File
Dynamic Shipping Fee Based on Custom Radio Buttons in Woocommerce
How to Set Cron Job Through PHP Script
How to Install Composer on App Service
Send Email with Attachment Using PHP
Cross Domain Ajax Request with Jquery/Php
How to Remove 'Index.Php' from Url in Codeigniter
Can't Set PHP Cookie on the Same Page
Disable Add to Cart Button for an Array of Products Ids in Woocommerce