Reputation: 296
I try to pass array using jquery by ajax and back but my code not working well. I all the time change it but it is not working. please advise what to do. I think my jquery code is ok but php code not replay. thx.
html code.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
var allVals = {
'a': '1',
'b': '2',
'c': '3'
};
//$.each(allVals, function(key, value) {
//alert(key + ': ' + value);
// });
});
$.ajax({
type: "POST",
dataType: 'html',
url: "test4.php",
data: 'allVals=' + allVals,
cache: false,
success: function(data)
{
alert(data);
$('#test').html(data);
}
});
</script>
<title>Insert title here</title>
</head>
<body>
<div id="test">##</div>
</body>
</html>
php code test4.php
<?php
$allVals = $_POST['allVals'];
if ($allVals != ""){
foreach ($allVals as $key => $value) {
echo ($key.' '.$value."<br />");
} }
?>
Upvotes: 2
Views: 2846
Reputation: 46602
You can also send an array to PHP from jQuery like this. In your example your sending an object:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
header('Content-Type: text/html');
print_r($_POST['allVals']);
/*
Array ( [0] => 1 [1] => 2 [2] => 3 )
*/
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function() {
var myJavascriptArray = new Array('1', '2', '3');
//or var myJavascriptArray=["1","2","3"];
$.ajax({
type: "POST",
dataType: 'html',
url: "test.php",
data: {'allVals[]=' : myJavascriptArray},
cache: false,
success: function(data){
$('#test').html(data);
}
});
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Upvotes: 3
Reputation: 1341
You are doing the $.ajax-call outside the $(document).ready(...-scope. Move it inside!
I would do this part
data: 'allVals=' + allVals
like so:
data: allVals
And if you want to receive it as you do in php, change it to:
data: {"allVals": allVals}
Hope I got the syntax correctly.. ;)
Upvotes: 1
Reputation: 16269
You would be better using jQuery $.POST method:
var javascriptArray = new Array('a', 'b', 'c');
$.post('url_to_test4.php', {'allVals[]': javascriptArray }, function(data){
// perform some action with the response data.
});
This will send to the PHP file an array named allVals
, that will have the contents of your javascriptArray
.
Upvotes: 0
Reputation: 3833
If you need a php array, I advise you to do like this :
data: {allVals : allVals}
The $_POST['allVals']
will contains an array.
Otherwise, If you only need single parameters you have to do as follows :
var stringToSend = sep = "";
$.each(allVals, function(key, value) {
stringToSend += sep+key+"="+value;
sep = "&";
});
$.ajax({
type: "POST",
dataType: 'html',
url: "test4.php",
data: stringToSend,
...
Upvotes: 0
Reputation: 1635
data: 'allVals=' + allVals,
this is the culprit, what gets sent is allvals[object Object]
Upvotes: 0