user3089871 user3089871 - 5 months ago 9
jQuery Question

Send two arrays with Ajax to php, cant get response

So, i make two arrays each containing all the id's i need

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

});
}


});


I try to send confirmList and refuserList to php, which seems to work but once i'm in php

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

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

echo $accepter;
echo $refuser;


It send me back those errors :

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

What do i have to use to reach the arrays in php ?

EDIT

The problem is solved thanks to first comment.
In the beginning i didnt put the [] in the ajax request

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


So i tried to json_parse, json_decode, but it never seemed to work. When i finaly put the [] i was still trying all the json_decode stuff, but whitout it it works perfectly so final php is simply :

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


var_dump($_POST['confirmList']);

echo $accepter[0];

Answer
$_POST['confirmList']

Its array in itself. Dont need to json_decode() this.