This article mainly introduces the $. ajax method in jquery to submit forms. I hope it will be helpful to you.
The Code is as follows:
Function postdata () {// submit data function
$. Ajax ({// call jquery's ajax Method
Type: "POST", // set the ajax method to submit data
Url: "OK. php", // submit the data to OK. php
Data: "writer =" + $ ("# writer"). val (), // the value in the writer field in the input box is used as the submitted data.
Success: function (msg) {// callback after successful submission. The msg variable is output by OK. php.
Alert ("data submitted successfully"); // if necessary, you can display the msg variable value to a DIV element.
}
});
}
Jquery manual description:
The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processData option to disable automatic conversion. It must be in Key/Value format. If it is an array, jQuery automatically corresponds to the same name for different values. For example, {foo: ["bar1", "bar2"]} is converted to '& foo = bar1 & foo = bar2 '.
Example:
The Code is as follows:
$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});
Here, the parameters following data can be written in two forms: one is to write common url parameters, and the other is to write them in a json array,
The data section in the preceding example can also be written as follows: data: {name: "John", location: "Boston "}. What are the differences between the two statements?
Today, I found a slight difference in the usage of the two in development. First, we use the url to pass the parameter. If the parameter contains the "&" symbol, the parameter may not be received or is incomplete, for example, "data: "name = John & location = Boston ",",
If the name value is "john & smith", this may cause a problem. We can escape it using the encodeURIComponent () method in JS,
However, if you use data: {name: "John", location: "Boston"}, you do not need to escape it. If you escape it, you will receive the escaped string.