Reputation: 363
I have this array in javascript:
arr = [1, "string", 3]
And this, my ajax call:
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
}
});
Here's the php excerpt:
<?php
$arr = $_POST['client_info'];
// I want to access the array, indexed, like this:
$arr[0] = 2;
$arr[1] = "Hello";
$arr[2] = 10000;
?>
But I get this error:
Uncaught TypeError: Illegal invocation jquery-1.8.3.min.js:2
What's the correct way to do this? Thanks!
Upvotes: 0
Views: 5824
Reputation: 20199
Remove client_info[]
to client_info
and extra brace
Try
<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
{'client_info': arr},
function(data) {
// some stuffs here
}
);
</script>
Upvotes: 0
Reputation: 100175
extra braces, change:
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
}
});
to
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
// } <--remove this
});
Upvotes: 1
Reputation: 14523
Remove }
from });
to remove syntax error.
Also no need to use []
with client_info
so you can remove it.
Use:
<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
{'client_info': arr},
function(data) {
// some stuffs here
}
);
</script>
ajax_book_client.php
<?php
$arr = $_POST['client_info'];
echo $arr[0];
echo $arr[1];
echo $arr[2];
?>
Upvotes: 2