2

I'm working on Data-tables with PHP.

I have a table in first column there is a check box which says Select All and every row has a checkbox. datatable image

When a user clicks on the select all check box only those checkbox values of row visible will be fetched and shown in php after is click on submit.

PHP output when clicked on select all

The issue is when i select a checkbox from page no 2 say i have checked on Australia with id 13 Clicked on Australia and i go to page no 3 and select nothing. Then click on submit i do not get the $_POST field name with the is of Australia.PHP out put when row checked box is selected and move to next page

But when is but when check on Australia and click submit without changing the page then it will fetch me its $_POST details. Clicked on Australia and then submit

I want to know is their a way the get checkbox data to php when i have selected multiple checkbox from different pages.

I'm using latest datatable js files.

Images are used to let developer understand.

Below is my HTML CODE

view.php

<

?php
require 'conn.php';

$sql = "SELECT * FROM tbl_country";
$result = $conn->query($sql);

$country_array = array();

if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        $country_array[] = $row;
    }
} else {
    echo "0 results";
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
    </head>
    <body>
        <br>
        <br>

        <div class="container">
            <form action="process.php" method="post">
                <input type="submit" name="submit" value="submit">
                <table class="table table-bordered" id="example" >
                    <thead>
                        <tr>
                            <th>
                                <label for="select_all">
                                    <input type="checkbox" id="select_all" onclick="selectAll()" name="select_all" value="1" > Select All
                                </label>
                            </th>
                            <th>id</th>
                            <th>name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($country_array as $value) {
                            ?>
                            <tr>
                                <td>
                                    <label for="select_one_<?php echo $value['country_id']; ?>">
                                        <input type="checkbox" class="common_class" id="select_one_<?php echo $value['country_id']; ?>" name="select_one_<?php echo $value['country_id']; ?>" value="select_one_<?php echo $value['country_id']; ?>">
                                    </label>
                                </td>
                                <td><?php echo $value['country_id']; ?></td>
                                <td><?php echo $value['country_name']; ?></td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
            </form>
        </div>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js">
        </script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js">
        </script>
        <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js">
        </script>

        <script>
            $(document).ready(function () {
                $('#example').DataTable({
                    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
                });
            });

            function selectAll() {
                var is_checked = document.getElementById('select_all').checked;

                if (is_checked) {
                    $('.common_class').prop('checked', true);
                } else {
                    $('.common_class').prop('checked', false);
                }

                console.log(is_checked);
            }
        </script>
    </body>
</html>

PHP FILE process.php code

    <?php
    echo '<pre>';
    print_r($_POST);
?>

php file conn.php

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tempco_cms";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

Thanks In Advance

2
  • Possible duplicate of How to submit checkboxes from all pages with jQuery DataTables Commented Mar 27, 2018 at 9:53
  • @ Terry Thanks for quick response, Say user have checked to one of the checkbox but want to uncheck it should i provided an extra button for that or is their a better solution. Commented Mar 27, 2018 at 10:11

2 Answers 2

0

Use serializeArray and ajax to submit all checkbox (checked/unchecked)

var frmData = $("#my_form").serializeArray(); //my_form the id of your form
$(".common_class").each(function(){
 frmData.push({name: "country_selection[]", value: $(this).val()}); 
});

$.ajax({
 url: "your_action_url",
 type: 'POST',
 data: frmData,
 cache: false,
 success: function(result) {  
 },
 error: function(result) {
  }
 }); 

That will post all your form fields and a new object array which is named country_selection[]

and in your php you can now iterate it using

$checkbox = $_POST['common_selection'];
for($i=0;$i<count($checkbox);$i++){
echo $checkbox[$i];
}
Sign up to request clarification or add additional context in comments.

Comments

0
$('#deleteTriger').on("click", function(event){ // triggering delete one by one
        if( $('.deleteRow:checked').length > 0 ){  // at-least one checkbox checked
            var ids = [];
            $('.deleteRow').each(function(){
                if($(this).is(':checked')) {
                    ids.push($(this).val());
                }
            });
            var ids_string = ids.toString();  // array to string conversion
            console.log(ids_string);
            $.ajax({
                type: "POST",
                url: "/getIds.php",
                data: {data_ids:ids_string},
                success: function(result) {
                    table.draw(); // redrawing datatable
                },
                async:false
            });
        }
    });

and in php file get all selected ids like this $data_ids = $_POST['data_ids'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.