Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a jQuery-function that passes a list of objects to my server via AJAX:

var pList = $(".post:not(#postF)").map(function() {
  return this.id;
});
    $.ajax({
        type: "POST",
        url: "refresh",
        data: "pList="+pList,
...

on PHP-side, I need to check if a certain needle is in that array, I tried the following:

$pList = $_POST['pList'];

if(in_array('p82', $pList)){
    error_log('as');
}

which does not work, although "p82" is in the array. Maybe the jQuery object is not a real array or not passed to PHP as an array? Thanks for any help.

share|improve this question
    
you cannot send array to php via ajax.. you will have to make it as json and send the json which you can jeson_decode on php side.. –  Dinesh May 1 '13 at 9:32
    
You can simply add var_dump($_POST['pList']); to your PHP script and then have a look what it outputs via developers.google.com/chrome-developer-tools/docs/network or Firebug. And yes, you need to serialize your list. –  MartyIX May 1 '13 at 9:33
    
so "data: "pList="+JSON.stringify(pList)" and decoding it server-side should make it better? –  Raphael Jeger May 1 '13 at 9:39

1 Answer 1

up vote 1 down vote accepted

Add .get() to the end of your map function:

var pList = $(".post:not(#postF)").map(function() {
  return this.id;
}).get();

Then stringify the pList object:

data: 'pList=' + JSON.stringify(pList)

Decode the json server-side:

json_decode($_POST['pList']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.