i want to post an array from java script to php by ajax. But i don't know how do that, especially send it to php function like controller class. Correct me if i'm wrong, this is my java script source, as a function to send an array :
<script>
function send(){
var obj = JSON.stringify(array);
window.location.href = "post.php?q=" + obj;
}
</script>
i was try, but still fail. really need help..
asked Mar 30, 2015 at 7:24
Ugy Astro
4253 gold badges7 silver badges18 bronze badges
5 Answers 5
As described in the JQuery API documentation, you can use
var rootPath="http://example.com/"
var jsonData = $.toJSON({ q: array });
var urlWS = rootPath + "post.php";
$.ajax({
url: urlWS,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonData,
success: function(result) {
// do something here with returned result
}
});
answered Mar 30, 2015 at 7:38
Matt
27.4k19 gold badges131 silver badges202 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
var array= [];
array[0] = 'hi';
array[1] = 'hello';
$.ajax({
url: 'http://something.com/post.php',
data: {array: array},
type: 'POST'
});
answered Mar 30, 2015 at 7:28
Vladu Ionut
8,2231 gold badge24 silver badges30 bronze badges
Comments
try like this,
var data_to_send = $.serialize(array);
$.ajax({
type: "POST",
url: 'post.php',
data: data_to_send,
success: function(msg){
}
});
or
you can pass as json like below,
$.ajax({
type: "POST",
url: 'post.php',
dataType: "json",
data: {result:JSON.stringify(array)},
success: function(msg){
}
});
answered Mar 30, 2015 at 7:29
Ayyanar G
1,5451 gold badge11 silver badges24 bronze badges
1 Comment
Ugy Astro
is possible to call function of specified class by ajax?
var arr = <?php echo json_encode($postdata); ?>;
ajax: {
url:"post.php"
type: "POST",
data: {dataarr: arr},
complete: function (jqXHR, textStatus) {
}
You can try this .this will work
answered Mar 30, 2015 at 7:30
krishna
1461 gold badge1 silver badge13 bronze badges
Comments
example
ajax code:
$.ajax({
url: 'save.php',
data: {data: yourdata},
type: 'POST',
dataType: 'json', // you will get return json data
success:function(result){
// to do result from php file
}
});
PHP Code:
$data['something'] = "value";
echo json_encode($data);
answered Mar 30, 2015 at 7:33
Wahyu Kodar
6741 gold badge6 silver badges15 bronze badges
Comments
default