Php ajax returns a json data instance

Source: Internet
Author: User

This tutorial is an example of php ajax returning json data. It uses ajax to accept and process data requests sent from the json. php file in real time.

Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>




Php tutorial ajax return webpage special effects on data instance <script type = "text/" language = "javascript">
Var xmlhttp;
Function createxmlhttprequest ()
{
// Var xmlhttp = null;
Try
{
// Firefox, opera 8.0 +, safari
Xmlhttp = new xmlhttprequest ();
}
Catch (e)
{
// Internet explorer
Try
{
Xmlhttp = new activexobject ("msxml2.xmlhttp ");
}
Catch (e)
{
Xmlhttp = new activexobject ("microsoft. xmlhttp ");
}
}
Return xmlhttp;
}
Function startrequest (id)
{
Createxmlhttprequest ();
Try
{
Url = "json. php? Cid = "+ id;
Xmlhttp. onreadystatechange = handlestatechange;
Xmlhttp. open ("post", url, true );
Xmlhttp. send (null );
}
Catch (exception)
{
Alert ("xmlhttp fail ");
}
}
Function handlestatechange ()
{
If (xmlhttp. readystate = 4)
{
If (xmlhttp. status = 200 | xmlhttp. status = 0)
{
Var result = xmlhttp. responsetext;
Var json = eval ("(" + result + ")");
Alert ('name: '+ json. name );
Alert ('Age: '+ json. age );
Alert ('Id: '+ json. id );
}
}
}
</Script>

 






 

Json. php file

/*************************************** ***********************
*
* Use a specific function to process all elements in the array
* @ Param string & $ array the string to be processed
* @ Param string $ function the function to be executed
* @ Return boolean $ apply_to_keys_also whether it is also applied to the key
* @ Access public
*
**************************************** *********************/
Function arrayrecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
Static $ recursive_counter = 0;
If (++ $ recursive_counter> 1000 ){
Die ('possible deep recursion attack ');
}
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
Arrayrecursive ($ array [$ key], $ function, $ apply_to_keys_also );
} Else {
$ Array [$ key] = $ function ($ value );
}

If ($ apply_to_keys_also & is_string ($ key )){
$ New_key = $ function ($ key );
If ($ new_key! = $ Key ){
$ Array [$ new_key] = $ array [$ key];
Unset ($ array [$ key]);
}
}
}
$ Recursive_counter --;
}

/*************************************** ***********************
*
* Convert an array to a json string (compatible with Chinese characters)
* @ Param array $ array the array to be converted
* @ Return string the converted json string
* @ Access public
*
**************************************** *********************/
Function json ($ array ){
Arrayrecursive ($ array, 'urlencode', true );
$ Json = json_encode ($ array );
Return urldecode ($ json );
}

$ Array = array
(
'Name' => 'chia ',
'Age' => 20,
'Id' => $ _ post ['cid']
);


 

Echo json ($ array );
/*********
{"Name": "Sia", "age": "20 "}
 
This tutorial is an example of php ajax returning json data. It uses ajax to accept and process data requests sent from the json. php file in real time.

This tutorial

Http://down.php100.com/down/code/jquery/2010/0812/20181.html

***********/
?>


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.