jquery Ajax two ways to submit form data __javascript

Source: Internet
Author: User

It seems that Ajax is getting more and more hot, as a Web application developers if not this feeling will be outdated, and may even be in the job when the elimination of repeated. I am also a Web application developer, of course, I also want to "drift" one, or the job is not guaranteed AH.

Before implementing Ajax using a JavaScript script one knock it out, it's tedious. After learning jquery It is not so difficult to realize Ajax, of course, apart from the jquery framework there are other excellent frameworks here I will focus on the more popular jquery. There are two ways of Jquery Ajax submission form, one is the URL parameter submission data, and the other is form submission (peace is always the same in the background can get the value of form forms). In the form you want to submit, if there are a lot of elements suggested in the second way to submit, of course, if you want to practice the "typing level" in the first way to submit also may not be, I believe that developers do not want to pay white effort. No more nonsense to put the example.
First to download Jquery, jquery.form These two plug-ins, a lot of online.
One: URL parameter submission data

 
<script type = "Text/javascript" src = ". /js/jquery.js "></script> 
<script type=" text/javascript "> 
function Checkcorpid ()// Detect if the customer number is available 
{ 
if ($.trim ($ ("#txtF_CORPID") [0].value) = "")//txtf_corpid is the customer number input box 
{ 
alert ("Please enter customer number!"); 
} 
Else 
{ 
$ ("#checkFlag"). HTML ("detecting");//Display prompt information 
$.ajax ({ 
type: "Get", 
URL: "Checkcorpid.ashx", 
Data: "Id=" +$.trim ($ ("#txtF_CORPID") [0].value),//Submit form, equivalent to checkcorpid.ashx?id=xxx 
success:function (msg) {$ (" #checkFlag "). HTML (" "); alert (msg); Operation after the successful operation. MSG is a background pass value 
});} 
</script> 

Background code:

if (context. request.params["ID"]. ToString ()!= "")
{
Pxt.Logic.SYS.CORP_BASE_INFO Cbil = new Pxt.Logic.SYS.CORP_BASE_INFO ();
BOOL Flag=cbil.checkcorpid (context. request.params["ID"]. ToString ());
if (flag)
{context
. Response.Write ("The customer number is already occupied!");
else
{context
. Response.Write ("The customer number is available!");


Two: Form submission data
Foreground code:

<script type = "Text/javascript" src = ". /js/jquery.js "></script>//must refer to <script type =" Text/javascript "src =". /js/jquery.form.js "></script>//must reference <script type=" Text/javascript ">//wait for the DOM to is loaded $ (do cument). Ready (function () {$ (' #Tip '). Hide ()//The element that displays the action prompt is not visible $ (' #form1 '). Submit (function ()//Submitting form {//alert ("ddd"); var Options = {target: ' #Tip ',//background will assign the passed value to the element URL: ' Returnvisit.aspx?flag=do ',///to which execution type: ' POST ', success:function () {Alert ($ (' #Tip '). text ());
Show action Prompt};
$ (' #form1 '). Ajaxsubmit (options); return false;
In order not to refresh the page, return false, anyway, has been done in the background, nothing.
});
}
); </script> <body> <form id= "Form1" runat= "Server" > <div> <table width= "100%" border= "0" align
= "Center" cellpadding= "0" cellspacing= "0" > <tr> <td colspan= "2" class= "Tablecategory" > Customer return visit </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Customer name:</td> <td class= "TableBg2" > <asp: TextBox id= "Txtf_corpname" runat= "Server" ></asp:TextBox> </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Return visit Theme: </td> <td class= "TableBg2" > <asp:textbox id= "txtf_returnvisittitle" runat= "Server" ></asp: textbox> </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Contacts:</td> <td class= " TableBg2 "> <asp:textbox id=" Txtf_contractperson "runat=" Server "></asp:TextBox> </td> </tr > <tr> <td width= "30%" class= "TABLEBG1" > Contact title:</td> <td class= "TableBg2" > <asp:textbox Id= "Txtf_contractpersonposition" runat= "Server" ></asp:TextBox> </td> </tr> <tr> <td Width= "30%" class= "TABLEBG1" > Tel:</td> <td class= "TableBg2" > <asp:textbox id= "Txtf_contractphone" "runat=" server ></asp:TextBox> </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Return visit time:</td> <td class= "TableBg2" > <asp:textbox id= "txtf_returnvisitdate"runat= "Server" ></asp:TextBox> </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Return visit content:</td> <td class= "TableBg2" > <asp:textbox id= "txtf_returnvisitcontent" runat= "Server" ></ asp:textbox> </td> </tr> <tr> <td width= "30%" class= "TABLEBG1" > Return visit Related Documents:</td>
class= "TableBg2" > <asp:textbox id= "txtf_returnvisitfile" runat= "Server" ></asp:TextBox> </td> </tr> <tr> <td width= "30%" class= "TableBg1" > </td> <td class= "TableBg2" > <asp:button Id= "SUBMIT_BT" runat= "Server" text= "OK" cssclass= "button"/> <input type= "reset" class= "button" value= "Restore"/> &  lt;/td> </tr> </table> <span id= "Tip" ></span> </div> </form> </body>

Background code:

protected void Page_Load (object sender, EventArgs e) {{if (request.querystring["flag"]!= null && Request.querys tring["Flag"]. ToString () = = "Do" {Pxt.Logic.DBRec.ReturnVisit BLL = new Pxt.Logic.DBRec.ReturnVisit (); if (BLL. ADD (model (0)) > 0) {Response.Write ("Operation succeeded.")
");
Response.End (); 
/**////<summary>///According to different needs, set the model-> get model///</summary>///<param name= "id" >id value </param> <returns></returns> private Pxt.Model.DBRec.ReturnVisit Model (int id) {//Get form value string f_corpname = Reque St. form["Txtf_corpname"].
ToString (); String f_returnvisittitle = request.form["Txtf_returnvisittitle"].
ToString (); String F_contractperson = request.form["Txtf_contractperson"].
ToString (); String f_contractpersonposition = request.form["Txtf_contractpersonposition"].
ToString (); String f_contractphone = request.form["Txtf_contractphone"].
ToString (); DateTime f_returnvisitdate = DateTime.Parse (request.form["Txtf_returnvisitdate").
ToString ()); String f_reTurnvisitcontent = request.form["Txtf_returnvisitcontent"].
ToString (); String f_returnvisitfile = request.form["Txtf_returnvisitfile"].
ToString (); String refer = session["username"].
ToString ();
DateTime dotime = DateTime.Now.Date;
Pxt.Model.DBRec.ReturnVisit Model = new Pxt.Model.DBRec.ReturnVisit (); if (ID > 0)//modify the record, otherwise indicates that the increment record {model.id = ID;} model.
F_corpname = F_corpname; Model.
F_returnvisittitle = F_returnvisittitle; Model.
F_contractperson = F_contractperson; Model.
F_contractpersonposition = f_contractpersonposition; Model.
F_contractphone = F_contractphone; Model.
F_returnvisitdate = f_returnvisitdate; Model.
F_returnvisitcontent = f_returnvisitcontent; Model.
F_returnvisitfile = F_returnvisitfile; Model.
Refer = refer; Model.
Dotime = Dotime;
return model;  }

Note: Jquery.form is a plugin that Jquery uses to help manipulate the form

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.