JQuery Ajax json data provides code for saving
Jquery ajax webpage special effects on data provides saving code
<Html>
<Head>
<Title> jquery ajax webpage special effects: Saving code on data </title>
</Head>
<Script language = "webpage effects" src = "jquery. js"> </script>
<Script language = "webpage effect">
$ (Document). ready (function ()
{
$ ('# Send_ajax'). click (function (){
Var params = $ ('input'). serialize (); // serialized form value
Alert (params );
$. Ajax ({
Url: 'ajax _ json. php ', // background processing program
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>
Ajax_json.php File
<? Php
// $ Arr = $ _ post; // if you send data in the $. get () mode, change it to $ _ get. Or simply: $ _ request
$ Arr =$ _ request;
$ Arr ['append'] = 'qian ';
$ 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 );
}
}
?>
Download the jquery. js file to the official website.