2016-06-14 59 views
0

我做两个阵列,每个阵列包含所有所需的ID:发送两个数组与AJAX到PHP,不能得到响应

$('.valider').click(function(){ 
     var confirmList = []; 
     var refuserList = []; 
     var id; 

     /* CREATE refuserList ARRAY */ 
     $('input[type=checkbox][class=refuser]:checked').each(function() { 
      id = $(this).parent().parent().children('td:first-child').text();    
      refuserList.push(id); 
     }); 

     /* CREATE confirmList ARRAY */ 
     $('input[type=checkbox][class=confirm]:checked').each(function() { 
      id = $(this).parent().parent().children('td:first-child').text(); 
      confirmList.push(id); 

     }); 

     alert(confirmList); 
     alert(refuserList); 

     /* check if one of them has at least one element */ 
     if(confirmList.length > 0 || refuserList.length > 0){ 
      /* send info to php */ 
      $.post("confirm_points.php", { 'confirmList[]' : confirmList , "refuserList[]" : refuserList }) 
        .done(function(data){ 

         alert(data); 
         $('.test').html(data); 
        }); 
     } 
     }); 

我尝试发送confirmList和refuserList到PHP,这似乎是工作,但一旦我米PHP:

$accepter =json_decode($_POST['confirmList']); 
$refuser = json_decode($_POST['refuserList']); 

    var_dump($accepter); 
    var_dump($refuser); 

    echo $accepter; 
    echo $refuser; 

它返回此错误:

Warning: json_decode() expects parameter 1 to be string, array given in C:\wamp\www\JAUGE\confirm_points.php on line 5

我有什么用,以达到在PHP中的数组?

编辑

问题是解决了由于第一条评论。 一开始我并没有把[]在AJAX请求:

$.post("confirm_points.php", { 'confirmList[]' : confirmList , "refuserList[]" : refuserList }) 
        .done(function(data){ 

所以我试图json_parse,json_decode,但它似乎从来没有工作。当我终于把[]我还在尝试所有json_decode的东西,但它完美没有它那么最终的PHP很简单:

if(isset($_POST['confirmList'])){ 
     $accepter =$_POST['confirmList']; 
    } 
if(isset($_POST['refuserList'])){ 
     $refuser = $_POST['refuserList']; 
    } 


    var_dump($_POST['confirmList']); 

    echo $accepter[0]; 
+1

请分享结果:var_dump($ _ POST ['confirmList']); –

+0

哦,它把我送回阵列!好吧。谢谢哈哈哈。它完全可以正常工作 – Couteau

+0

?这个问题到底是什么? – Cherish

回答

1
$_POST['confirmList'] 

其本身阵列。不需要json_decode()这个。