How to Use Jquery to submit Form forms through Ajax _ jquery-js tutorial

Source: Internet
Author: User
There are many ways to submit Form forms. This article describes how Jquery submits Form forms through Ajax. Today, we can see Jquery's ajax method to submit data to the server. The original Article is:

Save the data to the server. If the data is successfully saved, the information is displayed.
JQuery code:

The Code is as follows:


$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});


Then I thought about it. Is there a way to submit the form? However, I cannot write var demo =$ ("# pname"). val (); for every fom input.
Later, today I saw a method called. map, which I thought about and could be used for reference;
The html code is as follows. Next I want to submit all input data whose id is dlg_form,

The Code is as follows:




Isn't it a lot? If you want to write every input, do you want to vomit blood?
Let's look at my method. First, we take down all the names and values of input,
The js Code is as follows:

The Code is as follows:


Var str_data = $ ("# dlg_form input"). map (function (){
Return ($ (this). attr ("name") + '=' + $ (this). val ());
}). Get (). join ("&");
Alert (data );


Ps: You alert, you will find that the architecture here is pname = xxx & pname2 = xxxx, etc,

Then let's look back at what ajax submitted:

The Code is as follows:


$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});


Have you found that we only need to put the obtained data in data?

The complete code should be

The Code is as follows:


$. Ajax ({

Var str_data = $ ("# dlg_form input"). map (function (){
Return ($ (this). attr ("name") + '=' + $ (this). val ());
}). Get (). join ("&");
Type: "POST ",
Url: "some. php ",
Data: str_data,
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});


OK, that's simple. If applicable, you can use it...

Haha.

If you have any questions, please submit them.
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.