PHP + JQuery + Json asynchronous data acquisition implementation AJAX

Source: Internet
Author: User

This article introduces the AJAX Implementation of PHP + JQuery + Json asynchronous data acquisition.

The javascript library jquery is used to asynchronously obtain data, and many people have used lightweight json to replace xml for data transmission. How can I use PHP + JQuery + Json to asynchronously obtain data and implement AJAX? The specific methods are listed below

The Code is as follows: Copy code
<Select name = "country" id = "country" onchange = "PostCountryJson ();">

In the select control, when the onchange event is triggered, we execute the JavaScript Function PostCountryJson ().

Followed by JavaScript code:

The Code is as follows: Copy code

Function PostCountryJson (){

Var mid = $ ("# country"). val ();
Var queryString = {"mid": mid}
Var url = "getCountrycode. <span class = "tag-span"> <a class = "tag" href = "http://www.bKjia. c0m "title =" view all articles in php "target =" _ blank "> php </a> </span> ";
$. GetJSON (url, queryString, function (json ){
$ ("# Tel_country_code"). get (0). value = json. countryCode;
});
}

 

Data is also obtained through the Json dataset.

The final step is the getCountrycode. php code:

The Code is as follows: Copy code

<? Php

Export de_once '../configure/conn. php'; // create a connection with mysql database
$ Country = $ _ GET ['mid ']; // obtain the mid parameter from PostCountryJson (), which is the city name.

// Perform a search to search information about the program in the country table
$ SelectStr = "SELECT * FROM country WHERE country_name = '". $ country ."'";
$ Result = mysql_query ($ selectStr, $ conn );

If ($ result ){
$ CountryCode = mysql_fetch_array ($ result );
}

$ CountryCodeStr = $ countryCode ['country _ Code']; // store the data in the country_code field to the variable


$ JsonArr = array ('countrycode' => $ countryCodeStr); // write an array in Jason format

$ JsonStr = json_encode ($ jsonArr); // encode Jason data. I didn't expect it to be simpler than. net.
Echo ($ jsonStr); // output Jason data
?>

In addition to the above method, there are many jquery ajax methods.

1. load (url, [data], [callback]): load the remote HTML file code and insert it into the DOM.

Url (String): the URL of the requested HTML page.

Data (Map): (optional) key/value data sent to the server.

Callback (Callback): (optional) callback function when the request is completed (success is not required.

By default, this method uses the GET method to pass data. If the [data] parameter passes data in, it is automatically converted to the POST method. In jQuery 1.2, you can specify a selector to filter loaded HTML documents. In DOM, only filtered HTML code is inserted. The syntax is like "url # some> selector ".

This method can easily dynamically load HTML files, such as forms.

Sample Code:

The Code is as follows: Copy code

$ (". Ajax. load"). load ("/2009/06/10/1500682 .html. post ",
Function (responseText, textStatus, XMLHttpRequest ){
This; // here this points to the current DOM object, that is, $ (". ajax. load") [0]
// Alert (responseText); // content returned by the request
// Alert (textStatus); // Request status: success, error
// Alert (XMLHttpRequest); // XMLHttpRequest object
});


JQuery. get (url, [data], [callback]): Use the GET Method for asynchronous requests.

JQuery. post (url, [data], [callback], [type]): Use the POST method for asynchronous requests.

JQuery. getScript (url, [callback]): load and execute a JavaScript file using GET requests.

JQuery. ajax (options): loads remote data through HTTP requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.