Open In App

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:

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






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

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




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

Output:
Before clicking on the button:

After clicking on the button:


Article Tags :