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..

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
    }
});
0
var array= [];
array[0] = 'hi';
array[1] = 'hello';

    $.ajax({
      url: 'http://something.com/post.php',
      data: {array: array},
      type: 'POST'
    });
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){

    }
});
1
  • is possible to call function of specified class by ajax? Commented Mar 30, 2015 at 7:33
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

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);

Your Answer

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.