为什么 Javascript 数组在 php 中不会崩溃


Why Javascript array doesn't implode in php?

我实际上有这个数组var checked_rls= ['24','56'];

            var checked_rls = []
            $("input[name='chk_cont[]']:checked").each(function ()
            {
                checked_rls.push($(this).val());
            });

然后我尝试使用 AJAX 传递它

  $.ajax(
            {
                type :"POST",
                url : "sale_ajax.php",
                data: 
                {
                    rls :  checked_rls,
                    rls_date : $("#rls_date").val()
                },
                success: function(msg)
                {
                    alert('saved successfully');
                },
                error: function()
                {
                    alert("Failure");
                }
            });

然后我收到了sale_ajax.php文件中的数组,就像这样$cont = $_POST['rls'];

现在的问题是我可以在forEach函数中运行这个数组,但是当我试图将这个数组内爆成一个字符串时,它只返回一个值。

例如,在这种情况下,我通过了 24,56,当我尝试implode(",",$cont)时; 它只返回第一个值 24:

var_dump($cont) output is
array(2){[0]=>string(2) "24" [1]=>string(2) "56"} 

PHP代码是:

if(isset($_POST['rls']))
    {
        $rls_date = $_POST['rls_date'];
        $cont = $_POST['rls'];
        $rls_cont_sold = implode(",",$cont);
         $rls_insert = mysql_query("INSERT INTO release_details (release_date, cont_sold_id) VALUES (STR_TO_DATE('$rls_date', '%d-%m-%Y'), '$rls_cont_sold')")or die(mysql_error());          
         $id = mysql_insert_id();
        foreach($cont as $val)
        {       
          $cont_sold_update = "UPDATE cont_sold SET release_details_id = '$id' WHERE cont_sold_id = '$val'";
          $insert = mysql_query($cont_sold_update)or die(mysql_error());         
        }
    }

var_dump($_POST)是

array(2){ 
["rls"]=>(array(2){[0]=>string(2) "24" [1]=>string(2) "56"})
["rls_date"]=>string(10) "10-04-2015"
}

在这种情况下到底发生了什么?谢谢

在元素

click事件中放入以下代码chk_count

$("input[name='chk_cont[]']").click(function(){         
          var checked_rls = [];
            $("input[name='chk_cont[]']:checked").each(function ()
            {
                checked_rls.push($(this).val());
            });
});

与其传递数组,不如传递字符串本身,它会减少步骤数。

       var checked_rls = "";
        $("input[name='chk_cont[]']:checked").each(function ()
        {
            checked_rls += $(this).val()+",";
        });