-1

I have jQuery to get multiple checkbox value.

You can refer for the demo here

The function for that jQuery is OK, when We tick the checkbox then We can see what We choose based on data-id after btnUpdate click.

But now I want to pass and store it to database using PHP by Ajax. So Example output,

1 -> read
1 -> update
2 -> update

Then save it to database on table:

ID | chkStatus
1  | read
1  | update
2  | update

Here is the HTML

<table>
<tr>
  <th>Nama</th>
  <th>Create</th>
  <th>Read</th>
  <th>Update</th>
  <th>Delete</th>
</tr>
<tr>
  <td>coba</td>
  <td><input type="checkbox" data-id="1" data-tipe="create"></td>
  <td><input type="checkbox" data-id="1" data-tipe="read"></td>
  <td><input type="checkbox" data-id="1" data-tipe="update"></td>
  <td><input type="checkbox" data-id="1" data-tipe="delete"></td>
</tr>
<tr>
  <td>coba 2</td>
  <td><input type="checkbox" data-id="2" data-tipe="create"></td>
  <td><input type="checkbox" data-id="2" data-tipe="read"></td>
  <td><input type="checkbox" data-id="2" data-tipe="update"></td>
  <td><input type="checkbox" data-id="2" data-tipe="delete"></td>
</tr>
<tr>
  <td><input type="button" id="btnUpdate" value="Update"/>
</tr>

jQuery

$(function(){
  $('#btnUpdate').click(function(){
    var cb = [];
    $.each($('input[type=checkbox]:checked'), function(){
      cb.push($(this).data('id') + ' -> ' +$(this).data('tipe'));
    });
    $('#status').val(cb.join("\n"));
  })
});
HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

1 Answers1

2

You can send arrays server side via both get or post, in your case here I'd recommend modifying how you're building the array mind:

$(function(){
    $('#btnUpdate').click(function(){
        var cb = [],
            post_cb = []

        $.each($('input[type=checkbox]:checked'), function(){
            var id = $(this).data('id'),
                tipe = $(this).data('tipe')

            cb.push(id + ' -> ' + tipe);
            post_cb.push({
                'id': id,
                'tipe': tipe
            });
        });
        $('#status').val(cb.join("\n"));

        $.ajax({
            'type': 'post',
            'url': '/path/to/script.php',
            'data': {
                'cb': post_cb
            },
            'success': function(response) {
                // Do something
            },
            'error': function(response) {
                // Do something
            }
        });
    })
});

And then in your PHP file:

<?php

print_r($_POST['cb']);
/*

Array
(
    [0] => Array
        (
            [id] => 1
            [tipe] => read
        )

    [1] => Array
        (
            [id] => 1
            [tipe] => update
        )

    [2] => Array
        (
            [id] => 2
            [tipe] => update
        )

)

*/

?>
Scoots
  • 3,048
  • 2
  • 21
  • 33
  • Thanks for the answer, how to store it to database? if I have table column: ID | CHKVALUE – HiDayurie Dave Mar 04 '17 at 06:38
  • What database technology are you using? MySQL? I'm asking because depending on the database, the PHP functions you use will be different – Scoots Mar 05 '17 at 12:22
  • Hi Scoots, I'm using Oracle DB – HiDayurie Dave Mar 06 '17 at 01:26
  • Hey again, sorry for the delay in replying to this. I'm afraid I have no experience working with Oracle DB and as such am uncomfortable writing out sample code for you. However, I can point you at the relevant PHP docs, which have a few examples of common queries: http://php.net/manual/en/function.oci-connect.php – Scoots Mar 13 '17 at 10:05