jQuery | post() Method

The post() method in jQuery loads the page from server using POST HTTP request and returns XMLHttpRequest object.

Syntax:

 $.post( url, data, callback_function, data_type )

Parameters: This method accepts four parameters as mentioned above and described below:



  • url: It is the required parameter and used to send the request.
  • data: It is optional parameter and it represents key/value pairs of data that will be sent to the server.
  • callback_function: It is optional parameter and it represents a function to be executed when the data is loaded successfully.
  • data_type: It is optional parameter and it represents a type of data to be returned to callback function: xml, html, script, json, jsonp, or text.

main.php This PHP file call in the below example when button pressed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    echo "Hello Geeks!";
?>

chevron_right


Example: This example use post() method and call a PHP file.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        jQuery post() Method
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h2 id="gfg">GeeksforGeeks</h2
      
    <button id="b">
        Click Here!
    </button
      
    <!-- Script to use post() method -->
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                $.post("/submit.php", {
                    name: "GFG",
                },
                  
                function(data,status) {
                    document.getElementById("gfg").innerHTML
                            = data;
                    document.getElementById("b").innerHTML
                            = "Data Passed";
                });
            });
        });
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:



My Personal Notes arrow_drop_up

Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.