foreach checkbox POST in php
foreach($_POST['checkbox'] as $value) {
}
Note that $_POST['checkbox']
will only exist if at least one checkbox is checked. So you must add an isset($_POST['checkbox'])
check before that loop. The easiest way would be like this:
$checkboxes = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $value) {
// here you can use $value
}
getting a checkbox array value from POST
Your $_POST array contains the invite array, so reading it out as
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
?>
won't work since it's an array. You have to loop through the array to get all of the values.
<?php
if(isset($_POST['invite'])){
if (is_array($_POST['invite'])) {
foreach($_POST['invite'] as $value){
echo $value;
}
} else {
$value = $_POST['invite'];
echo $value;
}
}
?>
PHP - posting checkbox values foreach record
I resume my comments here.
First of all if a checkbox is not checked the array is not created.
So if I check only kirana on the first line and i check mall in the second.
Your code will provide this values:
$_POST['kirana'][0] = 1
$_POST['mall'][0] = 1
that's why you got trouble when you insert data.
An easy way to fix this and fit to your code is to force indexes on each line.
<!-- First Line -->
<input type="checkbox" name="kirana[0]" id="kirana_0" value="1" />
<input type="checkbox" name="chemist[0]" id="chemist_0" value="2" />
<!--Second line-->
<input type="checkbox" name="kirana[1]" id="kirana_1" value="1" />
<input type="checkbox" name="chemist[1]" id="chemist_1" value="2" />
Now if we examine another time the first example with this HTML
//Now indexes are correct
$_POST['kirana'][0] = 1
$_POST['mall'][1] = 1
Note id attribute on html must be unique,else you must use class attribute.
Hope this helps.
Create a dynamic array from PHP &_POST for checked boxes to use in a foreach loop
First, some code indentation would probably help read your code easier.
Next, change your form checkboxes to submit as an array instead of individual variables.
<form action="output.php" method="post">
<fieldset>
<legend>Enter Email Address</legend>
<input type="text" name="email" size="30" value="Enter Email Address">
<br>
</fieldset>
<fieldset>
<legend>Select Container Type</legend>
<input type="checkbox" name="containers[]" value="7z" checked> 7zip
<input type="checkbox" name="containers[]" value="dmg"> dmg
<input type="checkbox" name="containers[]" value="gz"> gz
<input type="checkbox" name="containers[]" value="rar" checked> rar
<input type="checkbox" name="containers[]" value="tar"> tar
<input type="checkbox" name="containers[]" value="zip" checked> zip<br>
</fieldset>
<fieldset>
<legend>Select Script Type</legend>
<input type="checkbox" name="scripts[]" value="bat" checked>bat
<input type="checkbox" name="scripts[]" value="ps1" checked>ps1
<input type="checkbox" name="scripts[]" value="py">py
<input type="checkbox" name="scripts[]" value="sh">sh<br>
</fieldset>
<fieldset>
<legend>Select Macro Type</legend>
<input type="checkbox" name="macros[]" value="docm" checked>docm
<input type="checkbox" name="macros[]" value="xlsm" checked>xlsm<br>
</fieldset>
<br>
<input type="submit" value="Send" />
</form>
Now you can easily loop through all of the values selected by the user.
<?php
foreach($_POST['containers'] as $container) {
//...
}
foreach($_POST['scripts'] as $script) {
//...
}
foreach($_POST['macros'] as $macro) {
//...
}
?>
HTML will only pass checked values to the server by default.
How to Get Checkbox values in PHP using FOR loop only
$skills=$_POST['skills'];
for($i=0; $i<sizeof($skills);$i++){
echo $skills[$i];
};
Retrieve Checked Checkboxes with PHP and post
The first issue I see from looking at your code is your use of the equality operator (==
) instead of the assignment operator (=
) when setting your variables.
This:
$edges == 'yes';
Should be:
$edges = 'yes';
It's hard to tell from your question whether your issue simply comes down to this.
You state you're using that code for each of your checkboxes. All of your checkbox conditions can go inside the single submit isset()
block.
E.g.
if ( isset( $_POST['submit'] ) ) {
// test for edges...
// test for light...
}
Given your markup, you should be using isset()
for the checkboxes. If you post the form and those checkboxes aren't checked, they won't be set in $_POST
.
Complete example:
if ( isset( $_POST['submit'] ) ) {
$edges = ( isset( $_POST['edges'] ) ) ? 'yes' : 'no';
$light = ( isset( $_POST['light'] ) ) ? 'yes' : 'no';
// . . .
}
While the approach above using single variables works, there's a much cleaner way of writing this with arrays.
Array example HTML:
<input type="checkbox" name="myCheckbox[]" value="edges">
<input type="checkbox" name="myCheckbox[]" value="light">
. . .
Array example PHP:
if ( isset( $_POST['submit'] ) ) {
// Each checkbox option should default to no.
$checkboxes = array(
'edges' => 'no',
'light' => 'no',
// . . . don't forget to add all your options here.
);
// Let's make sure we have some checkboxes posted.
if ( ! empty( $_POST['myCheckbox'] ) ) {
// Loop through each checked option.
foreach ( (array) $_POST['myCheckbox'] as $checked ) {
// If the checked option exists in our checkbox array, update value to yes.
if ( array_key_exists( $checked, $checkboxes ) ) {
$checkboxes[ $checked ] = 'yes';
}
}
}
// Do stuff with your checkbox values here...
}
Submit an array checkbox although it's not checked
There is at least two solutions
1) You should pass an index of row to form field name for each row. For example
<tr>
...
<input type="text" name="description_of_goods_fa[1]"
class="form-control">
...
</tr>
<tr>
...
<input type="text" name="description_of_goods_fa[2]"
class="form-control">
...
</tr>
So in the PHP code you can check in the loop for the existence of index as
$hazardous = isset($_POST['hazardous'][$index]) ? $_POST['hazardous'][$index] : null;
2) You can place a hidden input with name hazardous in addition to checkbox
<input type="checkbox" class="form-control hazardous-checkbox"
onclick="changeHazardousInput(this)" value="1">
<input type="hidden" name="hazardous[]"
onchange="check_hazardous(this);">
So then you just need to write a 'changeHazardousInput' function that changes sibling hidden input's value
Example of the changeHazardousInput function:
function changeHazardousInput(checkbox) {
checkbox.nextElementSibling.value = checkbox.checked ? checkbox.value : '';
}
Related Topics
Pdo Lastinsertid() Always Return 0
PHP Include File in Webroot from File Outside Webroot
PHP Undefined Index Error $_Files
Fatal Error: Call to a Member Function Prepare() on a Non-Object In
Display an Array in a Readable/Hierarchical Format
Laravel Middleware with Multiple Roles
How to Save PHP Session Data to a Database Instead of in the File System
How to Save Changed Simplexml Object Back to File
File Download Script Doesn't Work When Called from Ajax
How to Decode a JSON String in PHP
PHP Removing HTML Tags from String
Loading CSV into MySQL Table with PHP