Simple form submission using Jquery ajax

Source: Internet
Author: User

In jquery, we can directly use $. post or $. ajax to submit a form. The following section describes the ajax submission form instance.

This is the simplest way.

 

The Code is as follows: Copy code
$. Post ("/index. php? Action = ajax & rs = TWProAjax: checkProTermsExisted ", $ (this). serialize (), function (data ){
}, 'Json ');

Note that serialize submits all the input in your form in the form of post or get. See the following example.


We have a very common form:

First, create a new login.html page:

The Code is as follows: Copy code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> $. ajax () method to send a request </title>
<Script type = "text/javascript" src = "Scripts/jquery-1.4.1.js"> </script>
<Style type = "text/css">
Body
{
Font-size: 13px;
}
. DivFrame
{
Width: 225px;
Border: solid 1px #666;
}
. DivFrame. divTitle
{
Padding: 5px;
Background-color: # eee;
Height: 23px;
}
. DivFrame. divTitle span
{
Float: left;
Padding: 2px;
Padding-top: 5px;
}
. DivFrame. divContent
{
Padding: 8px;
Text-align: center;
}
. DivFrame. divContent. clsShow
{
Font-size: 14px;
Line-height: 2.0em;
}
. DivFrame. divContent. clsShow. clsError
{
Font-size: 13px;
Border: solid 1px # cc3300;
Padding: 2px;
Display: none;
Margin-bottom: 5px;
Background-color: # ffe0a3;
}
. Txt
{
Border: #666 1px solid;
Padding: 2px;
Width: 150px;
Margin-right: 3px;
}
. Btn
{
Border: #666 1px solid;
Padding: 2px;
Width: 50px;
}
</Style>
<Script type = "text/javascript">
$ (Function (){
$ ("# TxtName"). focus (); // enter the focus
$ ("# TxtName"). keydown (function (event ){
If (event. which = "13") {// enter the Enter key, move the cursor to the password box
$ ("# TxtPass"). focus ();
}
});
$ ("# TxtPass"). keydown (function (event ){
If (event. which = "13") {// enter the Enter key and submit the form using. ajax
$ ("# BtnLogin"). trigger ("click ");
}
});
$ ("# BtnLogin"). click (function () {// "Log on" button to click the event
// Obtain the user name
Var strTxtName = encodeURI ($ ("# txtName"). val ());
// Obtain the input password
Var strTxtPass = encodeURI ($ ("# txtPass"). val ());
// Start sending data
$. Ajax
({// Login request processing page
Url: "Login. aspx", // logon processing page
DataType: "html ",
// Send request data
Data: {txtName: strTxtName, txtPass: strTxtPass },
Success: function (strValue) {// data returned after Successful Logon
// Display the status based on the returned value
If (strValue = "True") {// note that it is True, not true
$ (". ClsShow" Login .html ("Operation prompt, login successful! "+ StrValue );
}
Else {
$ ("# DivError" cmd.show(cmd.html ("Incorrect username or password! "+ StrValue );
}
}
})
})
})
</Script>
</Head>
<Body>
<Form id = "frmUserLogin">
<Div class = "divFrame">
<Div class = "divTitle">
<Span> User Logon </span>
</Div>
<Div class = "divContent">
<Div class = "clsShow">
<Div id = "divError" class = "clsError">
</Div>
<Div>
Name: <input id = "txtName" type = "text" class = "txt"/> </div>
<Div>
Password: <input id = "txtPass" type = "password" class = "txt"/> </div>
<Div>
<Input id = "btnLogin" type = "button" value = "login" class = "btn"/> & nbsp; & nbsp
<Input id = "btnReset" type = "reset" value = "cancel" class = "btn"/>
</Div>
</Div>
</Div>
</Div>
</Form>
</Body>
</Html>

Create Login. aspx to receive and process data.

The Code is as follows: Copy code

: <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Login. aspx. cs" Inherits = "JSDemo. Login" ResponseEncoding = "gb2312" %>

 
<%
String strName = System. Web. HttpUtility. UrlDecode (Request ["txtName"]);
String strPass = System. Web. HttpUtility. UrlDecode (Request ["txtPass"]);
Bool login = false;
If (strName = "admin" & strPass = "admin ")
{
Login = true;
}
Response. Write (login );
%>


The most important part is the following code.

The Code is as follows: Copy code

$ ("# BtnLogin"). click (function () {// "Log on" button to click the event
// Obtain the user name
Var strTxtName = encodeURI ($ ("# txtName"). val ());
// Obtain the input password
Var strTxtPass = encodeURI ($ ("# txtPass"). val ());
// Start sending data
$. Ajax
({// Login request processing page
Url: "Login. aspx", // logon processing page
DataType: "html ",
// Send request data
Data: {txtName: strTxtName, txtPass: strTxtPass },
Success: function (strValue) {// data returned after Successful Logon
// Display the status based on the returned value
If (strValue = "True") {// note that it is True, not true
$ (". ClsShow" Login .html ("Operation prompt, login successful! "+ StrValue );
}
Else {
$ ("# DivError" cmd.show(cmd.html ("Incorrect username or password! "+ StrValue );
}
}
})

Related Article

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.