admin管理员组文章数量:1315061
I want to post an array to PHP using AJAX and on success returns the value back to JavaScript. Here's my code.
JavaScript:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "phparray.php",
data: {
array1: phparray
},
success: function(data){
alert("success");
alert(data);
}
});
});
HTML:
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script>
var phparray = jQuery.makeArray();
for(var i=0; i<10 ; i++){
phparray.push(i);
}
</script>
<script type="text/javascript" src="phparraypost.js"></script>
</head>
<body>
</body>
</html>
PHP:
<?php
$n=$_POST['array1'];
echo $n;
?>
The data I get back says
<br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\php\phparray.php</b> on line <b>4</b><br /> Array
and I don't have an idea what might be wrong with it.
The HTML, PHP and JavaScript code are in different files.
I want to post an array to PHP using AJAX and on success returns the value back to JavaScript. Here's my code.
JavaScript:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "phparray.php",
data: {
array1: phparray
},
success: function(data){
alert("success");
alert(data);
}
});
});
HTML:
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script>
var phparray = jQuery.makeArray();
for(var i=0; i<10 ; i++){
phparray.push(i);
}
</script>
<script type="text/javascript" src="phparraypost.js"></script>
</head>
<body>
</body>
</html>
PHP:
<?php
$n=$_POST['array1'];
echo $n;
?>
The data I get back says
<br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\php\phparray.php</b> on line <b>4</b><br /> Array
and I don't have an idea what might be wrong with it.
The HTML, PHP and JavaScript code are in different files.
Share Improve this question edited Jul 27, 2013 at 13:47 user188654 asked Jul 27, 2013 at 13:27 L4redsL4reds 4122 gold badges5 silver badges21 bronze badges 6- 2 So what isn't working exactly. Other than using jQuery to create an empty array, there doesn't seem to be anything wrong? – adeneo Commented Jul 27, 2013 at 13:30
- The 'path' between a client (javascript) and the server (php) is HTTP. Http doens't care about arrays and only accepts strings. In your javascript you should format the array to a string (ma seperated, JSON) and parse it serverside to get the array back. – Joren Commented Jul 27, 2013 at 13:30
- @Joren - jQuery will do that for you. – adeneo Commented Jul 27, 2013 at 13:31
- @adeneo that's why I didn't state is as an answer but as a ment. – Joren Commented Jul 27, 2013 at 13:32
- the data i get back says "<br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\php\phparray.php</b> on line <b>4</b><br /> Array" and i don't have any idea about it. – L4reds Commented Jul 27, 2013 at 13:37
4 Answers
Reset to default 5The problem is in your PHP - you are using echo on an array. Instead use var_dump.
<?php
$n=$_POST['array1'];
var_dump($n);
?>
I came up with the solution using $.param
. This function is used internally to convert form element values into a serialized string representation. I am not sure if it fulfills your requirement.
<script>
$(document).ready(function(){
var phparray = new Object();
for(var i=0; i<10 ; i++){
phparray['val' + i] = i; //store value in object
}
$.ajax({
type: "POST",
url: "phparray.php",
data: {
array1: $.param(phparray) //serialize data
},
success: function(data){
alert("success");
alert(data);
}
});
});
</script>
PHP
$pieces = explode('&', $_POST['array1']); //explode passed serialized object
$phparray = array();
foreach($pieces as $piece){
list($key, $value) = explode('=', $piece);
$phparray[$key] = $value; //make php array
}
var_dump($phparray);
http://api.jquery./serializeArray/ does this for you, since your already using jquery..
use this:
$("#formId").submit(function(event) {
event.preventDefault();
var url = $(this).attr('action');
$.ajax({
url : url,
data : $(this).serialize(),
cache : false,
contentType : false,
processData : false,
type : 'POST',
success : function(data) {
$('#result').html('<div class="notes">Done</div>');
}
});
return false;
});
本文标签: javascriptHow to post an array using AJAX to PHPStack Overflow
版权声明:本文标题:javascript - How to post an array using AJAX to PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741972036a2407899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论