Treat
Treat

Reputation: 192

POST json data using form

I need to pass JSON data to a server. Data looks like this:

{"method":"like","data":{"type":1,"id":123}}

I need it to be done automaticaly, if I would use a form it can be done like this:

<script>
jQuery(document).ready(function(){
jQuery("#form").submit();
});
</script>

But I need to pass data using $_POST in that exact format. How can this be done?

Upvotes: 2

Views: 668

Answers (1)

Anonymous
Anonymous

Reputation: 3689

see jquery's load() or better (post() if you don't need to load anything to the page you're working on) to pass the json object with POST

If you don't know how to convert a string/array to json, use phps json_encode

Example:

$(document).ready(function(){
    $.post("script.php", { <?php echo $string; ?> },
       function() {
         alert("Your test.php page received the string!);
       });
});

That way the $string gets loaded automatically when the page is loaded (if user has javascript turned on).

Upvotes: 2

Related Questions