2017-03-03 116 views
-1

我有jQuery获取多个复选框的值。jQuery通过多个复选框值使用Ajax的PHP到

您可以参考的演示here

为jQuery的功能是确定的,当我们勾选复选框,然后我们可以看到我们选择基于btnUpdate点击后数据-ID。

但现在我想通过并将其存储到数据库使用Ajax的PHP。 所以示例输出,

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

然后将其保存到数据库中的表:

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

下面是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")); 
    }) 
}); 
+0

http://stackoverflow.com/questions/42530480/how-do-i-pass-jquery-value-to-php/42531178#42531178 –

+2

[用PHP jQuery的Ajax的POST示例](的可能的复制HTTP ://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) –

回答

1

您可以通过同时发送阵列服务器端获取或交的,你的情况在这里我建议修改你如何构建阵列记:

$(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 
      } 
     }); 
    }) 
}); 

然后在你的PHP文件:

<?php 

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

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

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

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

) 

*/ 

?> 
+0

感谢您的回答,浩把它存储到数据库?如果我有表格列:ID | CHKVALUE –

+0

您使用的是哪种数据库技术? MySQL的?我在问,因为根据数据库的不同,你使用的PHP函数会有所不同 – Scoots

+0

Hi Scoots,我正在使用Oracle DB –