The HTML code is as follows:
| The code is as follows |
Copy Code |
|
<!doctype html>
<meta charset= "GBK" >
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<title>Document</title>
<script src= "Jquery.js" ></script>
<script>
$ (document). Ready (function () {
$ ("#button"). Click (function () {
$.post ("Ajax.php?id=1", function (data) {
$ ("#test"). val (data);
Console.log (data);
$.each (Data,function () {
alert (this);
});
});
});
});
</script>
<body>
<input type= "text" name= "test" value= "id=" "Test"/>
<button id= "button" > Trigger ajax</button>
</body>
The PHP code is as follows:
<?php
Print_r ($_get);
$data = Array (
' Name ' => ' AAA ',
' title ' => ' I-i-i '
);
$arr =json_encode ($data);
File_put_contents ("Ajax.js", $arr);
Echo $arr;
?>
|
Example 2
| The code is as follows |
Copy Code |
|
Here's all the code:
<title>jquery Ajax Example Demo </title>
<script language= "javascript" src= ". /lib/jquery.js "></script>
<script language= "JavaScript" >
$ (document). Ready (function ()
{
$ (' #send_ajax '). Click (function () {
var params=$ (' input '). Serialize (); Serializing the value of a form
$.ajax ({
ckeditor/"target=" _blank ">fckeditor/editor/' ajax_json.php '" >url: ' ajax_json.php ',//Background handler
Type: ' Post ',//Data send mode
DataType: ' json ',//Accept data format
Data:params,//data to be passed
Success:update_page//Return functions (here is the name of the function)
});
});
$.post () Way:
$ (' #test_post '). Click (function () {
$.post (
' Ajax_json.php ',
{
username:$ (' #input1 '). Val (),
age:$ (' #input2 '). Val (),
sex:$ (' #input3 '). Val (),
job:$ (' #input4 '). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval (' myjson= ' + data + '; ');
$ (' #result '). HTML ("Name:" + myjson.username + "<br/> Work:" + myjson[' job '));
}
);
});
$.get () Way:
$ (' #test_get '). Click (Function ()
{
$.get (
' Ajax_json.php ',
{
username:$ ("#input1"). Val (),
age:$ ("#input2"). Val (),
sex:$ ("#input3"). Val (),
job:$ ("#input4"). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval ("myjson=" + Data + ";");
$ ("#result"). HTML (myjson.job);
}
);
});
});
function Update_page (JSON)//Return functions entity, parameter is Xmlhttprequest.responsetext
{
var str= "Name:" +json.username+ "<br/>";
str+= "Age:" +json.age+ "<br/>";
str+= "Sex:" +json.sex+ "<br/>";
str+= "Work:" +json.job+ "<br/>";
str+= "Append test:" +json.append;
$ ("#result"). 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> Enter Name: </span><input type= "text" name= "username" id= "input1"/></p>
<p><span> input Age: </span><input type= "text" Name= "ages" id= "Input2"/></p>
<p><span> Enter Gender: </span><input type= "text" name= "Sex" id= "INPUT3"/></p>
<p><span> input work: </span><input type= "text" name= "job" id= "Input4"/></p>
</form>
<button id= "Send_ajax" > Submit </button>
<button id= "Test_post" >post submit </button>
<button id= "Test_get" >get submit </button>
</body>
PHP file ajax_json.php:
<?php
$arr = $_post; If you send the data in $.get (), 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);
}
}
?> |