0

I am sending an array to php script using an Ajax call, but I am not able to access that array in php.

Here is my code:

seats = ["s4","s6","s9","s24"];
sendBookedSeats(seats);

function sendBookedSeats(seats){
    console.log(seats);
    $.ajax({
      type: "POST",
      url: "index.php",
      data: {
        'seats' : seats,
      }
    })
}

How do I access the array seats in my php script?

6
  • 1
    Can you do var_dump($_POST); in index.php? Commented Jun 14, 2015 at 8:57
  • add contentType property Commented Jun 14, 2015 at 8:58
  • You need to add more context, when are sending the data, are you using a form? And in what file is this code located, is that also index.php? Commented Jun 14, 2015 at 9:04
  • possible duplicate of How to search through a JSON Array in PHP Commented Jun 14, 2015 at 9:25
  • also see php.net/json_decode Commented Jun 14, 2015 at 9:25

2 Answers 2

1

Send data as json:

seats= ["s4","s6","s9","s24"];
sendBookedSeats(seats);



function sendBookedSeats(seats){
    console.log(seats);
    $.ajax({
      type: "POST",
      url: "index.php",
      data: {
        'seats' : JSON.stringify(seats),
      }
    })
}

PHP:

json_decode($_POST["seats"]);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Hannes, non of the answer is accepted. Did you get the solution yet or not. Did you try my code?
0
function sendBookedSeats(seats){
    $.ajax({
      type: "POST",
      contentType: 'application/json',
      url: "index.php",
      data: {
        'seats' : seats,
      }
    })
}

Add content Type to your ajax call.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.