I will simplify the code in order to highlight what I need:
<form id="highlighttable" name=form1 method="POST">
<div class="form-group">
<button name="save_multicheckbox" class="btn-primary">Save Checkbox</button>
</div>
<?php
if(isset($_POST['save_multicheckbox'])){
echo "<p>yes</p>";
$checklist = $_POST['ckb'];
foreach($checklist as $list) {
echo "<p>as</p>";
}
}
?>
<table>
<tr id="col-title">
<th>Title</th>
<th>Type</th>
<th>Post Date</th>
<th>Url</th>
<th>Feature</th>
</tr>
<?php foreach($result as $res){
?><tr>
<td><h6>Lorem</h6></td>
<td><h6>Lorem</h6></td>
<td><h6>Lorem</h6></td>
<td><h6>Lorem</h6></td>
<td id="h-center"><input type="checkbox" name="ckb[]" onclick="chkcontrol(<?php echo $res->chkbox_num; ?>)">
</td>
</tr>
<?php
} ?>
</table>
</form>
I have foreach from my select query, which outputs table rows below:
<?php foreach($result as $res){} ?>
output:
what I am trying to do is to echo or retrieve the rest of the rows on each selected row by marking what I want to get with a checkbox (last column)
<td id="h-center"><input type="checkbox" name="ckb" onclick="chkcontrol(<?php echo $res->chkbox_num; ?>)"></td>
my problem is that my foreach does not output the selected rows with that I tried below:
if(isset($_POST['save_multicheckbox'])){
echo "<p>yes</p>";
$checklist = $_POST['ckb'];
foreach($checklist as $list) {
echo "<p>as</p>";
}
}
first I tried to add an echo inside the button to check if it works and it does, I am just wondering how to show the selected rows. Is there maybe an issue with the variable I am using to get it? my attempt is $checklist = $_POST['ckb'];
where ckb
is the name of the checkbox.
Clarification: the screenshot below shows my foreach inside my onclick
they have a value starting from 0
2
Answers
You should your foreach loop to only display the rows that have been selected by checking if their corresponding checkbox has been checked.
You can do this by adding an if statement inside your loop like this:
There are several options.
First you need to chnage rendering part of table with checkbox id.
You have to use $res->id because you will save data whatever you change on checkbox.
If you wanna get rest of columns by making selection, then you need to handle this part at js function named chkcontrol().Because if you wanna get something of rest columns, you have to use js code and then send request.
Now you can output data from post request.