JQuery is an excellent Javascript framework that provides excellent packaging of js and many convenient functions. JQuery's ajax packaging is also excellent.
JQuery can transmit data using the json File Transfer Protocol (similar to xml, and has a tendency to replace xml), and the website background code must be used with it. PHP uses the json_encode function to encode the returned array data. However, this function is only supported by PHP5.2 and later versions.
I found a json operation class on the Internet and passed the test in PHP4.4.7. I have also created a function my_json_encode ($ phparr) to make the code compatible with PHP5.2 and later versions.
Sample Code (including json class package software) can be downloaded at: http://www.bkjia.com/uploadfile/2012/0221/20120221090101730.rar
All the code is as follows:
<Html>
<Head>
<Title> jQuery Ajax instance demonstration </title>
</Head>
<Script language = "javascript" src = "../lib/jquery. js"> </script>
<Script language = "javascript">
$ (Document). ready (function ()
{
$ ('# Send_ajax'). click (function (){
Var params = $ ('input'). serialize (); // serialized form value
$. Ajax ({
Url: 'ajax _ json. php', // background Handler
Type: 'post', // data transmission method
DataType: 'json', // accept the data format
Data: params, // data to be transmitted
Success: update_page // return function (function name here)
});
});
// $. Post () method:
$ ('# Test_post'). click (function (){
$. Post (
'Ajax _ json. php ',
{
Username: $ ('# input1'). val (),
Age: $ ('# input2'). val (),
Sex: $ ('# input3'). val (),
Job: $ ('# input4'). val ()
},
Function (data) // return function
{
Var myjson = '';
Eval ('myjson = '+ data + ';');
Certificate ('{result'}.html ("name:" + myjson. username + "<br/> job:" + myjson ['job']);
}
);
});
// $. Get () method:
$ ('# Test_get'). click (function ()
{
$. Get (
'Ajax _ json. php ',
{
Username: $ ("# input1"). val (),
Age: $ ("# input2"). val (),
Sex: $ ("# input3"). val (),
Job: $ ("# input4"). val ()
},
Function (data) // return function
{
Var myjson = '';
Eval ("myjson =" + data + ";");
$ ("# Result" cmd.html (myjson. job );
}
);
});
});
Function update_page (json) // return the function entity. The parameter is XMLhttpRequest. responseText.
{
Var str = "name:" + json. username + "<br/> ";
Str + = "age:" + json. age + "<br/> ";
Str + = "Gender:" + json. sex + "<br/> ";
Str + = "job:" + json. job + "<br/> ";
Str + = "append test:" + json. append;
$ ("# Result" example .html (str );
}
</Script>
<Body>
<Div id = "result" style = "background: orange; border: 1px solid red; width: 300px; height: 200px;"> </div>
<Form id = "formtest" action = "" method = "post">
<P> <span> input name: </span> <input type = "text" name = "username" id = "input1"/> </p>
<P> <span> input age: </span> <input type = "text" name = "age" id = "input2"/> </p>
<P> <span> input Gender: </span> <input type = "text" name = "sex" id = "input3"/> </p>
<P> <span> input job: </span> <input type = "text" name = "job" id = "input4"/> </p>
</Form>
<Button id = "send_ajax"> submit </button>
<Button id = "test_post"> POST submission </button>
<Button id = "test_get"> GET submit </button>
</Body>
</Html>
PHP file ajax_json.php:
<? Php
// $ Arr = $ _ POST; // if you send data in the $. get () mode, change it to $ _ GET. Or simply: $ _ REQUEST
$ Arr =$ _ REQUEST;
$ Arr ['append'] = 'test string ';
// Print_r ($ arr );
$ Myjson = my_json_encode ($ arr );
Echo $ myjson;
Function my_json_encode ($ phparr)
{
If (function_exists ("json_encode "))
{
Return json_encode ($ phparr );
}
Else
{
Require_once 'json/json. class. php ';
$ Json = new Services_JSON;
Return $ json-> encode ($ phparr );
}
}
?>
From chaojie2009