1

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

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0

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

1 Comment

is possible to call function of specified class by ajax?
0
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

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.