如何通过JQUERY将多个复选框传递给PHP

如何通过JQUERY将多个复选框传递给PHP

本文介绍了如何通过JQUERY将多个复选框传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经修改了我的代码,我使用firebug console.log来检测php是否通过数组的天气.并且萤火虫显示此内容-rescheck [] = 2& rescheck = 1& rescheck = 3

I HAVE modified my code, i used firebug console.log to detect weather the the php gets the array passed or not. and firebug displays this - rescheck[]=2&rescheck=1&rescheck=3

我认为如果php中的数组应该是什么样子,php就会得到数组.

I think php gets the array if THATS what an array in php supposed to be like.

SO伙计们,如果那是正确的,如何在数据库中插入该数组?或如何循环?我的foreach循环无效.

SO guys, if thats correct how to insert that array in database? or how to loop it? the foreach loop ive made didnt work.

JQUERY代码:

$('#res-button').click(function (){

var room_id=$('[name=rescheck[]]:checked').serialize().replace(/%5B%5D/g,'[]');

    alert(room_id);


  $.ajax({
                    type: "POST",
                    url: "reservation-valid.php",
                    data: {name_r:name_r, email_r:email_r,contact_r:contact_r,prop_id:p_id,cvalue:room_id},
                    success: function(data) {

                    console.log(data);
}

});
});

<input type="checkbox" name="rescheck[]" value="<?php echo $roomid; ?>"  />

PHP代码:

$c_array=$_POST['cvalue'];

    echo $c_array;

    //foreach($c_array as $ch)
    //{
    //$sql=mysql_query("INSERT INTO reservation VALUES('','$prop_id','$ch','$name_r','$contact_r','$email_r','')");
    //}

我认为我的jquery代码正确无误,但是我不知道如何使用PHP来获取它.

I think I managed my jquery code to be right, but I don't know how to fetch that with PHP.

推荐答案

这是我在网站上执行的操作,用于通过jQuery和Ajax调用将复选框列表保存到db.它创建一个数组,并将其传递给ajax调用,然后php脚本处理该数组.如果您在此处遇到任何错误,则应使用firebug调试js数组,以确保格式正确.

This is what I do ibn my site for saving to a db a list of checkboxes with jquery and ajax call. It make an array and pass it to the ajax call and the php script handle the array.If you get any error here you should debug the js array with firebug for be sure that is formed correctly.

js脚本:

var $checkBox = $('[name=rescheck[]]:checked');
$checkBox.each(function() {
   if ($(this).is(":checked")){
        valuesCheck[this.value] = 1;
    }else{
        valuesCheck[this.value] = 0;
   }

和PHP脚本:

$checkTab = $_POST['cvalue'];
        foreach ($checkTab as $idChkTab => $checkedOrNot){
            if ($checkedOrNot== "0"){
                //do something if isn't checked
            }

这篇关于如何通过JQUERY将多个复选框传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:20