0

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.

3
  • 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.. Commented May 1, 2013 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. Commented May 1, 2013 at 9:33
  • so "data: "pList="+JSON.stringify(pList)" and decoding it server-side should make it better? Commented May 1, 2013 at 9:39

1 Answer 1

1

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']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.