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
***********/
?>