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

Source: Internet
Author: User
Tags button type jquery library

There is a method in Jqery, $.post () to do a simple example of this method:

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

Parameters:

URL (String): The URL address where the request is sent.

data (MAP): Optionally, the data to be sent to the server is represented in the form of a Key/value key value.

Callback (Function): Optionally, the callback function (which is invoked only when the response return state is success) is loaded successfully.

type (String): (optional) The official note is: Type of data to be sent. The type that should actually be requested for the client (Json,xml, etc.)

1.html page (index.html)

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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 types returned here are: 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 Code code as follows:

<?php
$email = $_post["email"];
$address = $_post["Address"];

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


Description:When you click on the button, note that the button is now in the type buttons. When you do not use the $.post () method, the button type is submit, so submit the data in the form, and pass the Post method to the page action.php. At this point in the page action.php can accept the data passed over. When using the $.post method, we actually use the Post method in the function Ajax_post () method. (to refer to the jquery library file)

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.