dreamevil0002 2011-08-30 04:58
浏览 27
已采纳

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

I would like to save multiple checkbox(PHP and JQuery) in to MYSQL database. Example table test there are 4 fields. Column A, NAME, C AND D.

After I clicked save it will be saved to those table;

jQuery code:

$(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 code:

$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>
  • 写回答

1条回答 默认 最新

  • dqpu4988 2011-08-30 05:18
    关注

    I am not sure what exactly your question is, because in the title you said you need to save multiple checkboxes via jQuery and PHP. and in your PHP code you are selecting it. well you have made things quite complicated. assuming you want to save checkboxes value via jQuery to PHP.

    you need AJAX to save the values of checkboxes via jQuery. take for example see the html form below.

    <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>
    

    here is the jQuery Code.

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

    and in process.php you can do something like.

    if(isset($_POST['submit'])) {
        if(!empty($_POST['id'])) {
            $query = 'Your Insert Query or PHP logic goes here';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)