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);
asked Apr 10, 2012 at 6:44
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}.)
answered Apr 10, 2012 at 6:53
Sign up to request clarification or add additional context in comments.

Comments

2

you should use

$data=$_POST["arr"];
echo json_encode($data);
answered Apr 10, 2012 at 7:00

Comments

1

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

answered Apr 10, 2012 at 6:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.