A simple example of the $.post () method in jquery

Source: Internet
Author: User
Tags jquery library

There is a method in Jqery that $.post () makes a simple example of this method:

Jquery.post (URL, [data], [callback], [type]):
Use post to make asynchronous requests

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>untitled document</title>
<script type= "Text/javascript" src=\ ' #\ ' "/jquery-1.3.2.js" ></script>
<script language= "JavaScript" >
function Checkemail () {

if ($ (' #email '). val () = = "") {
$ (' #msg '). HTML ("Please enter the email!");
$ (' #email '). Focus;
return false;
}
if ($ (' #address '). val () = = "") {
$ (' #msg '). HTML ("Please enter the address!");
$ (' #address '). Focus;
return false;
}
Ajax_post ();
}

function Ajax_post () {
  $.post ("action.php", {email:$ (' #email '). Val (), address:$ (' # Address '). Val ()},
  function (data) {
   //$ (' #msg '). HTML ("Please enter the email!");
   //alert (data);
    $ (' #msg '). HTML (data);
 },
  "text");//The type returned here is: Json,html,xml,text
}
</script>

<body>
<form id= "Ajaxform" name= "Ajaxform" method= "post" action= "action.php" >
<p>
Email<input type= "text" name= "email" id= "email"/>

</p>
<p>
Address<input type= "text" name= "Address" id= "Address"/>
</p>
<p id= "MSG" ></p>
<p>
<input name= "Submit" type= "button" value= "Submit" onclick= "return Checkemail ()"/>
</p>
</form>
</body>


2.php page (action.php)

Copy CodeThe code is as follows:
<?php
$email = $_post["email"];
$address = $_post["Address"];

Echo $email;
Echo $address;
echo "Success";
?>


Note : When you click the button, notice that the button is now the type is button. When you do not use the $.post () method, the button type is submit, so that submit submits the data in the form, using the Post method to pass to the page action.php, At this point in the page action.php can receive the data sent. When using the $.post method, we actually use the Post method in the function Ajax_post () method. (To reference the jquery library file)

A simple example of the $.post () method in jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.