开发者

How to save multiple checkbox (jQuery and PHP)?

开发者 https://www.devze.com 2023-04-01 11:20 出处:网络
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.

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=""开发者_如何学Python 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>


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';
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号