Ajax in jquery Several methods of submitting forms (get, post two methods) _ajax related

Source: Internet
Author: User

In jquery The Ajax submission form has a post and get way, and we can use Ajax to serialize the form $ (Form ID) serialize () directly using the Get mode (); Here's the two ways to submit form data. $get Way to submit a form get () method via remote HTTP, let me introduce two methods of submitting form data.

Submit a Form $get way

The Get () method loads information through a remote HTTP GET request

Format

$ (selector). Get (Url,data,success (RESPONSE,STATUS,XHR), DataType)

Request test.php Web page, transfer 2 parameters, ignore return value:

$.get ("test.php", {name: "John", Time: "2pm"});

Displays the test.php return value (HTML or XML, depending on the return value):

$.get ("test.php", function (data) {
 alert ("Data Loaded:" + data);
});

Ajax serialization Form

$. Form.serialize (Namevaluepair)

Virtual a form, and set the name and value of the form control.

Parameters

Namevaluepair

Required option. Set up a virtual form control. This parameter is in the form of: {name1=value, name2=value2, ...}

return value

The string after the virtual form is serialized, in the form of: username=%e5%95%8a%e8%94%a1&password=123456

<form> 
<div><inputtype= "text" Name= "a" value= "1" id= "a"/></div> 
<div>< Inputtype= "text" name= "B" value= "2" id= "B"/></div> 
<div><inputtype= "hidden" name= "C" value= "3 "Id=" C "/></div> 
<div> 
<textareaname=" D "rows=" 8 "cols=" >4</textarea> 
" </div> 
<div><selectname= "E" > 
<optionvalue= "5" selected= "selected" >5</option > 
<optionvalue= "6" >6</option> 
<optionvalue= "7" >7</option> 
</select ></div> 
<div> 
<inputtype= "checkbox" Name= "F" value= "8" id= "F"/> 
</div> 
<div> 
<inputtype= "Submit" name= "G" value= "Submit" id= "G"/> 
</div> 
</ The form> 
. Serialize () method can manipulate JQuery objects that have selected individual form elements, such as <input>, <textarea>, and <select>. But the source sky, the choice <form> tag itself for serialization is generally easier: 
$ (' form '). Submit (function () { 
alert ($ (this). Serialize ()); 
Returnfalse; 
});

Output standard query string:

A=1&b;=2&c;=3&d;=4&e;=5

Submit a Form $POST way

$.post

Jquery.post (URL, [data], [callback], [Type]): Asynchronous request using POST method

Parameters:

URL (String): The URL address where the request is sent.
Data (MAP): (optional) The information to be sent to the server, expressed as a Key/value key-value pair.
Callback (function): (optional) the callback function (which is invoked only if the response return state is success) when loading succeeds.

$.post ("momsg.php", {"Tel": $ ("#username"). Val ()},function (data) {
if (data==0)//0 Success 1 unsuccessful 2 phone number format is not correct
  {   
   //  
  }  
});

Change the text of a DIV element through an AJAX POST request:

$ ("input"). KeyUp (function () {
 txt=$ ("input"). Val ();
 $.post ("demo_ajax_gethint.asp", {suggest:txt},function (result) {
  $ (' span '). HTML (result);
 };
});

Instance

<script type= "Text/javascript" > Function adddata () {var typename=$ ("#<%=this.typename.clientid%>"). Val (
   );
   var msg= "Not to be empty";
      if (typename== "") {if (msg!= "") {alert (msg);
     return false;
     } else {//Display progress bar $ ("#loading"). Ajaxstart (function () {$ (this). Show ();
     }); Event triggered before commit $ ("#msg"). Ajaxsend (function (request, settings) {$ (this). Append ("<li>starting request at" + settings.)
     URL + "</li>"); The Countryid here can dynamically take Var countryid= $ ("#<%=this.drpcountry.clientid%>") from the GridView. Val (); Get Drop-down menu value var cou Ntryname=format_get_name (Countryid)//Get Drop-down menu text var typeName = $ ("#<%=this.typename.clientid%>"). Val ()// Get txt typename value var Showtypedesc = $ ("#<%=this.showtypedesc.clientid%>"). Val (); Get txt Showtypedesc value// Invoke Juqery Ajax $.ajax ({type: "POST", url: "addnews.aspx", timeout:20000, Error:function () { Alert (' ERROR ');}, data: "countryid=" +countryid+ "&countryname=" +countryname+ "&typename=" +typename+ "&showTypeDesc=" +
      Showtypedesc, Success:function (msg) {var text=msg.split (' < '); Add an executed method $ ("#msg") when the Ajax request fails. Ajaxerror (function (request, settings) {$ (this). Append ("<li>error reques
      Ting page "+ Settings.url + </li>");
      });
      Add an executed method $ ("#msg") when the Ajax request succeeds. ajaxsuccess (function (request, settings) {$ (this). Append (Text[0]);
     });
      Empty the text inside the value $ ("#<%=this.typename.clientid%>"). Val ("");
      $ ("#<%=this.showtypedesc.clientid%>"). Val ("");
      return false;
   }
      });
    }//Get the text content of the Drop-down menu function format_get_name (id) {var DRP = $ (' <%=drpCountry.ClientID%> '); for (var i =0;i<drp.options.length;i++) {if (Drp.options[i].value = = ID) {return drp.optio
      Ns[i].text;
  } return '; } </script>

The above content is the cloud Habitat Community small make up to share jquery in the Ajax to submit a form several methods (get, post two methods), hope to be helpful to everyone.

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.