0

In my page I need to send an array in javascript to Phpscript. My code doesn't return a value and does not give an error. How can I do that?

arr[0]='one';
arr[1]='two';
arr[2]='three';
arr[3]='four';

$.post(
       'sort2.php', 
       {data:arr}, 
       function(result) {
                         alert(result[0]);                  
       },
       'json' 
);

In sort2.php

$data=$_GET["arr"];
echo json_encode($data);
3
  • 4
    Are you posting from client and looking for sent data in $_GET array? Commented Apr 10, 2012 at 6:46
  • yes, I want to get values of arr array in phpscript. Commented Apr 10, 2012 at 6:52
  • You are posting you should use $_POST array Commented Apr 10, 2012 at 7:10

3 Answers 3

3
  1. You got $_GET and $_POST mixed up in the PHP script.
  2. It should be $_POST['data'], not $_POST['arr'] (the arr array is passed under the key name data in your JavaScript code: {data:arr}.)
Sign up to request clarification or add additional context in comments.

Comments

2

you should use

$data=$_POST["arr"];
echo json_encode($data);

Comments

1

You are using POST method to send data. You should read the $_POST array

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.