skip to Main Content

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:

1

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

2

Answers


  1. 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:

    foreach($result as $res) {
      if(isset($_POST['ckb'][$res->id])) {
        // display the row here
      }
    }
    
    Login or Signup to reply.
  2. 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.

    <td id="h-center"><input type="hidden" name="ckb_data[<?php echo $res->chkbox_num; ?>]"><input type="checkbox" name="ckb[]" onclick="chkcontrol(<?php echo $res->chkbox_num; ?>)"></td>
    

    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.

    function chkcontrol(id){
       var data = '';
       $(this).parent().prevAll().each(function(){
           // get data of columns
       });
    }
    

    Now you can output data from post request.

    if(isset($_POST['save_multicheckbox'])){
        echo "<p>yes</p>";
        $checklist = $_POST['ckb'];
        $checklist_data = $_POST['ckb_data']
        foreach($reuslt as $row) {
            echo "<pre>";
            print_r($_POST['ckb_data'][$row->chkbox_num]);
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search