How to get values of multiple selected (dynamic) checkbox in php?
Use checkbox name as an array,
example :
<form method="post" action="" id="frm_id">
<input type="checkbox" name="chkid[]" value="10,Anu" />Anu
<input type="checkbox" name="chkid[]" value="11,Raj" />Raj
<input type="checkbox" name="chkid[]" value="12,Ram" />Ram
<input type="checkbox" name="chkid[]" value="13,xxx" />xxx
<input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
<input type="checkbox" name="chkid[]" value="15,zzz" />zzz
<input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
<input type="submit" value="Insert" name="sub"/>
</form>
<?php
if(isset($_POST['sub']))
{
$id=$_POST['chkid'];
for($i=0;$i<count($id);$i++)
{
$exp=explode(',',$id[$i]);//Explode id and name
echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
}
}
?>
Post dynamic checkbox ids with values
I don't know why you need this but in your case I would do this:
while($row = mysqli_fetch_array($result)) {?>
<br><input type="checkbox" name="categories_chk[<?= $row["cat_id"]; ?>]" value="<?= $row["cat_name"]; ?>"> <?= $row["cat_name"]; ?>
<?php
}
See, I set index of categories_chk
as a category id.
If you print_r($_POST['categories_chk'])
you will have a key=>value array where key is category id and value is it's name.
You can iterate over it kinda:
foreach ($_POST['categories_chk'] as $id => $name) {
echo 'Category id ' . $id . ' with name ' . $name;
}
Of course, I need to mention that $row["cat_id"]
must be unique for such approach, otherwise you will have two same keys in array and the latter overwrites the previous one.
SelectAll checkbox for the dynamic checkbox inputs
try this
<?php
$i=0;
while($data = mysql_fetch_array($subject)) {
echo "<input type='checkbox' id='{$i}' class='check'>"."CheckAll" ;
echo "<input type='checkbox' class='check_{$i}' value='{$data['OrderID']}'>" . $data['OrderID'] ;
echo "<input type='checkbox' class='check_{$i}' value='{$data['ProductID']}'>" . $data['ProductID'] ;
echo "<input type='checkbox' class='check_{$i}' value='{$data['UnitPrice']}'>" . $data['UnitPrice'] ;
echo "<input type='checkbox' class='check_{$i}' value='{$data['Quantity']}'>" . $data['Quantity'] . '</br>';
$i++;
}
?>
</form>
<script>
$('.check').click(function(e){
var id=$(this).attr('id');
$('.check_'+id).prop('checked',this.checked);
});
</script>
How to get values of dynamic Unchecked checkboxes in post PHP
How i said in the comment you can use array_diff
for select only the value isn't selected :
$dbarray = array('1','2','3','4');
$selectedarray = array('1','3');
$substract = array_diff($dbarray, $selectedarray);
print_r($substract); //print 2,4
Submitting a dynamic checkbox form and get values in pairs
From the discussion, to me it seems better to send the checkbox in key=>value
<input type="checkbox" name="valg[<?=$hent_data[id]?>]" value="<?=$hent_data[process_id]?>" />
This will give you a result in the php side similar to
Array
(
[valg] => Array
(
[uniqueKey3] => processID3
[uniqueKey7] => processID7
[uniqueKey8] => processID8
)
)
Therefore in the php you can do this:
foreach($_POST['valg'] as $ID => $processID){
echo $hest = "INSERT INTO chosen (kat_ref, prod_ref, process_id) VALUES ($ID, '', $processID)"."<br/>";
}
Related Topics
Check If a Specific Value Exists At a Specific Key in Any Subarray of a Multidimensional Array
Preventing Direct Access to a PHP Page, Only Access If Redirected
Get Total Number of Members in Discord Using PHP
Mysql Combine Two Columns into One in Query
Remove Comma on Last Item of Foreach
Codeigniter Call to a Member Function Num Rows on Boolean
Group Array Data on One Column and Sum Data from Another Column
Unset Post Variables After Form Submission
Codeigniter - Call to a Member Function Result() on Boolean In
How to Get Filename from a Variable (Url) in PHP
Auto Update Cart on Click in Woocommerce
Convert Accented Characters to Their Plain Ascii Equivalents
Character Encoding for French Accents
Setting Selected Option in Laravel Form
How to Resolve a Http 414 "Request Uri Too Long" Error