jQuery.post demo

How to Create a jQuery Ajax Post with PHP

In this short tutorial, we are going to share with you how to create a jQuery Ajax post request with PHP.

Also, you can see how to post JSON data with jQuery easily and quickly.

After checking out this tutorial, you will be able to create ajax post requests much more easily. What you need is looking through the options below.

jQuery Post Form Data with .Ajax() Method

Using the .ajax() method is one of the best solutions to the problem. Here is how you should act to create a jQuery post request with this method:

html> html> head> title>jQuery post form data with .ajax() method by codeofaninja.com title> style> div < margin: 10px; > style> head> body> h1>jQuery post form data with the .ajax() method h1> div>Filling out and submitting the form below to receive a response. div> form id="userForm" action="/form/submit" method="post"> div> input type="text" name="firstname" placeholder="Firstname" /> div> div> input type="text" name="lastname" placeholder="Lastname" /> div> div> input type="email" name="email" placeholder="Email" /> div> div> input type="submit" value="Submit" /> div> form> div id="response"> div> script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "> script> script> $(document).ready(function( ) < $('#userForm').submit(function( ) < // showing that something is loading $('#response').html("Loading response. "); /* * 'post_receiver.php' - where you will be passing the form data * $(this).serialize() - for reading form data quickly * function(data) <. - data includes the response from post_receiver.php */$.ajax(< type: 'POST', url: 'post_receiver.php', data: $(this).serialize() >) .done(function(data) < // demonstrate the response $('#response').html(data); >) .fail(function( ) < // if posting your form failed alert("Posting failed."); >); // to restrain from refreshing the whole page, it returns false; >); >); script> body> html>

JQuery Post Form Data with .Post() Method

The .post method is considered a shorthand for the .ajax method. Its syntax is more descriptive. See how to use it correctly:

html> html> head> title>jQuery post form data using .post() method by codeofaninja.com title> style> div < margin: 15px; > style> head> body> h1>jQuery post form data using .post() method h1> div>Filling out and submitting the form below to receive a response. div> form id="userForm" action="/form/submit" method="post"> div> input type="text" name="firstname" placeholder="Firstname" /> div> div> input type="text" name="lastname" placeholder="Lastname" /> div> div> input type="email" name="email" placeholder="Email" /> div> div> input type="submit" value="Submit" /> div> form> div id="response"> div> script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "> script> script> $(document).ready(function( ) < $('#userForm').submit(function( ) < // showing that something is loading $('#response').html("Loading response. "); /* * 'post_receiver.php' - where you will be passing the form data * $(this).serialize() - for reading form data quickly * function(data) <. - data includes the response from post_receiver.php */$.post('post_receiver.php', $(this).serialize(), function(data) < // demonstrate the response $('#response').html(data); >).fail(function( ) < //if posting your form fails alert("Posting failed."); >); // to restrain from refreshing the whole page, the page returns false; >); >); script> body> html>

jQuery Post JSON Data with .Post() Method

As an additional information, here, we provide you with an example on how to post JSON data. For that purpose, you need to generate a JSON string and post it as shown below:

html> html> head> title>jQuery post JSON data using .post() method by codeofaninja.com title> head> body> h1>jQuery post JSON data with .post() method h1> div>Click the button below to receive a response. div> input type="button" value="Post JSON" id="postJson" /> div id="response"> div> script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "> script> script> $(document).ready(function( ) < $('#postJson').click(function( ) < // showing that something is loading $('#response').html("Loading response. "); /* * 'post_receiver.php' - where you will be passing the form data * $(this).serialize() - for reading form data easily * function(data) <. - data includes the response from post_receiver.php */$.post('post_receiver.php', < user_id: "143", username: "ninjazhai", website: "https://codeofaninja.com/" >, function(data) < // demonstrate the response $('#response').html(data); >).fail(function( ) < // if posting your form failed alert("Posting failed."); >); // to restrain from refreshing the whole page page returns false; >); >); script> body> html>

Why to Use jQuery for Ajax Requests?

In this snippet, we represented to you several ways of making ajax post requests with jQuery. You may ask why to use it. The reason to use it in your practice is apparent: it provides you with the simplest, short and readable code.

Читайте также:  Uses for python scripts

About jQuery Ajax Get() and Post() Methods

These methods are used for requesting data from the server using HTTP get or post request.

They are mainly applied for implementing request and response between the client and the server.

Get is used for requesting data from a particular resource. Post is used for submitting data to be processed to a particular resource.

Источник

jQuery.post()

jQuery.post( url [, data ] [, success ] [, dataType ] ) Returns: jqXHR

Description: Send data to the server using a HTTP POST request.

version added: 1.0 jQuery.post( url [, data ] [, success ] [, dataType ] )

A callback function that is executed if the request succeeds. Required if dataType is provided, but can be null in that case.

The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

version added: 1.12-and-2.2 jQuery.post( [settings ] )

A set of key/value pairs that configure the Ajax request. All properties except for url are optional. A default can be set for any option with $.ajaxSetup(). See jQuery.ajax( settings ) for a complete list of all settings. Type will automatically be set to POST .

This is a shorthand Ajax function, which is equivalent to:

$.ajax(
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
>);

The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

Most implementations will specify a success handler:

$.post( "ajax/test.html", function( data )
$( ".result" ).html( data );
>);

This example fetches the requested HTML snippet and inserts it on the page.

Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

The jqXHR Object

As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," returned by $.post() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see Deferred object for more information). The jqXHR.done() (for success), jqXHR.fail() (for error), and jqXHR.always() (for completion, whether success or error; added in jQuery 1.6) methods take a function argument that is called when the request terminates. For information about the arguments this function receives, see the jqXHR Object section of the $.ajax() documentation.

The Promise interface also allows jQuery's Ajax methods, including $.get() , to chain multiple .done() , .fail() , and .always() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.

Источник

JQuery Ajax POST Method

JQuery Ajax POST Method

Sends an asynchronous http POST request to load data from the server. Its general form is:

jQuery.post( url [, data ] [, success ] [, dataType ] )
  • url : is the only mandatory parameter. This string contains the adress to which to send the request. The returned data will be ignored if no other parameter is specified
  • data : A plain object or string that is sent to the server with the request.
  • success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response.
  • dataType : The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html). if this parameter is provided, then the success callback must be provided as well.

Examples

$.post('http://example.com/form.php', );

requests form.php from the server, sending additional data and ignoring the returned result

$.post('http://example.com/form.php', , function(response)< alert("success"); $("#mypar").html(response.amount); >);

requests form.php from the server, sending additional data and handling the returned response (json format). This example can be written in this format:

$.post('http://example.com/form.php', ).done(function(response)< alert("success"); $("#mypar").html(response.amount); >);

The following example posts a form using Ajax and put results in a div

             

The following example use the github api to fetch the list of repositories of a user using jQuery.ajax() and put results in a div

             

jQuery.ajax()

$.post( url [, data ] [, success ] [, dataType ] ) is a shorthand Ajax function, equivalent to:

$.ajax() provides way more options that can be found here

More Information:

For more information, please visit the official website

Источник

Оцените статью