First know to Jquery.post (URL, [data], [callback], [type])
Describe the parameters:
URL: Sends the request address.
Data: Key/value parameter to be sent.
Callback: callback function when sending success.
Type: Returns the content format, XML, HTML, script, JSON, text, _default.
Description
Loads information through a remote HTTP POST request.
This is a simple POST request function to replace the complex $.ajax. Callback functions can be invoked when the request succeeds. If you need to perform a function when an error occurs, use $.ajax.
Let's take a look at a simple example
The code is as follows |
Copy Code |
<?php echo Json_encode (Array ("name" =>$_post[' name '));? > |
Then set up ajax.html file, note JS code: www.111cn.net
The code is as follows |
Copy Code |
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> <script type= "Text/javascript" src= "/jquery-1.7.1.min.js" ></script> <script> $ (document). Ready (function () { $ ("#sub"). Click (function () { $.post ("testpost.php", {name:$ ("#name"). Val ()},function (data,textstatus) { $ ("#result"). Append ("Data:" +data.name); $ ("#result"). Append ("<br>textstatus:" +textstatus); }, "JSON"); return false; }); }); </script> <body> <form action= "testpost.php" method= "POST" > <input type= "text" name= "name" id= "Name" > <input type= "Submit" id= "sub" value= "submitted" > </form> The contents of <div id= "Result" ></div> </body>
|
Usage 2: (click Post data to return data)
The code is as follows |
Copy Code |
<input type= "button" id= "Bnajax" value= "Ajax" onclick= "Ajaxtest ()"/> <script type= "Text/javascript" > function Ajaxtest () { $.post ("http://localhost:8012/t.asp", {"TXT": "123"},function (data) { $ ("#divMsg"). HTML (data); } ); } </script> |
Example 3
JS Code:
code is as follows |
copy code |
<script $ (document). Ready (function () { $ (". Ajax_btn"). Click (function () { $.post ("ajax.php",//Asynchronous Processing dynamic page {name:$ (". Name"). Val ()},//get the class named " The value of name literal, the value of function (data) {//data to return value, function to reverse value, $ (". Content"). val (data); When you get a return value, fill it in a text box with the class name "content" }); }) }) </script> |
ajax.php Code:
The code is as follows |
Copy Code |
<?php $name =$_post["name"]; if ($name = = "Netxu") { echo "I'm sorry," $name. " Data exists "; } else{ echo "Congratulations," $name. " can use "; } ?> |