如何保存多个复选框(jQuery和PHP)


How to save multiple checkbox (jQuery and PHP)?

我想保存多个复选框(PHP和JQuery)在MYSQL数据库。示例表测试有4个字段。A、NAME、C、d列

点击保存后,它将被保存到那些表中;

jQuery代码:

$(document).ready(function() {          
    $("#btnsave").click(function() {
        var test = [];
        $("input[name='aa[]']:checked").each(function() {
            test.push($(this).val());
        });                   
        alert(test);    
    });
});
function toggleCheckeda(status) {
    $(".chkaa").each( function() {
        $(this).attr("checked", status);
    })          
}
function toggleCheckedb(status) {
    $(".chkab").each( function() {
        $(this).attr("checked", status);
    })          
}
function toggleCheckedc(status) {
    $(".chkac").each( function() {
        $(this).attr("checked",status);
    })          
}
PHP代码:

$db->Connect(); 
$stmt = $db->ExecData("select id,A,name,C,D from tbl_check");
<form id="frm1" name="frm1">
    <table>    
        <tr>
         <td>
          <input type="checkbox" value="" onclick="toggleCheckeda(this.checked)">
         </td>
         <td></td>
         <td>
          <input type="checkbox" value="" onclick="toggleCheckedb(this.checked)">
         </td>
         <td>
          <input type="checkbox" value="" onclick="toggleCheckedc(this.checked)">
         </td>
        </tr>
        <tr>
            <th>
                A
            </th>
            <th>
                Name
            </th>
            <th>
                C
            </th>
            <th>
                D
            </th>
        </tr>
        <?php while ($row = $db->GetRow($stmt)){ ?>
        <tr>
            <td>
                <input type="checkbox" class="chkaa" name="aa[]" id="chk[]" value="a<?php echo $row["id"]; ?>" />
            </td>
            <td>
               <?php echo $row["name"]; ?>
            </td>
            <td>
           <input type="checkbox" class="chkab"  name="cc[]" id="chk[]" value="c<?php echo $row["id"]; ?>" />
            </td>
            <td>
                <input type="checkbox" class="chkac"  name="dd[]"  id="chk[]" value="d<?php echo $row["id"]; ?>" />
            </td>
        </tr>
       <?php } ?>
    </table>
   <input type="button" id="btnsave" name="btnsave" value="save" />
 </form>

我不确定你的问题到底是什么,因为在标题中你说你需要通过jQuery和PHP保存多个复选框。在PHP代码中选择它。你把事情弄得很复杂。假设你想通过jQuery将复选框的值保存到PHP.

需要AJAX通过jQuery保存复选框的值。以下面的HTML表单为例。

<form action="" method="post" id="save-something"/>
    <input type="checkbox" name="id[]" value="1"/>One
    <input type="checkbox" name="id[]" value="2"/>Two
    <input type="checkbox" name="id[]" value="3"/>Three
    <input type="checkbox" name="id[]" value="4"/>Four
    <input type="checkbox" name="id[]" value="5"/>Five
    <input type="checkbox" name="id[]" value="6"/>Six
    <input type="checkbox" name="id[]" value="7"/>Seven
    <button type="submit" name="save">Save</button>
</form>

这里是jQuery代码

$('#save-something').click(function(e){
    e.preventDefault();
    var formData = $(this).serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            alert('Sucessfully Saved');
        }
    });
});

在process.php中你可以这样做。

if(isset($_POST['submit'])) {
    if(!empty($_POST['id'])) {
        $query = 'Your Insert Query or PHP logic goes here';
    }
}