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
user1077300
4234 gold badges10 silver badges18 bronze badges
3 Answers 3
- You got
$_GETand$_POSTmixed up in the PHP script. - It should be
$_POST['data'], not$_POST['arr'](thearrarray is passed under the key namedatain your JavaScript code:{data:arr}.)
answered Apr 10, 2012 at 6:53
Etienne Perot
4,8928 gold badges43 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
you should use
$data=$_POST["arr"];
echo json_encode($data);
answered Apr 10, 2012 at 7:00
Alfred
21.5k64 gold badges175 silver badges258 bronze badges
Comments
You are using POST method to send data. You should read the $_POST array
answered Apr 10, 2012 at 6:52
Shiplu Mokaddim
57.5k20 gold badges147 silver badges193 bronze badges
Comments
lang-js
$_GETarray?$_POSTarray