If you are not familiar with the basic syntax of jquery, please search this site's tutorial resources. If you are unfamiliar with phpmailer usage, please see another article in this site, "Use the Phpmailer class library to send e-mail."
The first step is to create a form HTML page
Here, we show only the main part of the HTML structure code for the form:
Copy Code code as follows:
<div id= "Contact_form" >
<form name= "Contact" method= "POST" action= "" >
<fieldset>
<label for= "name" id= "Name_label" > Name </label>
<input type= "text" name= "name" id= "name" size= "" value= "" class= "Text-input"/> "
<label class= "error" for= "name" id= "Name_error" > This must be filled </label>
<label for= "Email" id= "Email_label" > Your email</label>
<input type= "text" name= "email" id= Email "size=" value= "" class= "Text-input"/>
<label class= "error" for= "email" id= "Email_error" > This must be filled </label>
<label for= "Phone" id= "Phone_label" > Your tel </label>
<input type= "text" name= "phone" id= "Phone" size= "value=" "class=" Text-input "/>"
<label class= "error" for= "Phone" id= "Phone_error" > This must be filled </label>
<br/>
<input type= "Submit" name= "Submit" class= "button" id= "submit_btn" value= "I want to send"/>
</fieldset>
</form>
</div>
A few notes:
This contains the entire containing information with an ID of contact_form, which is meaningful and will be used later when JavaScript interacts with the user.
It should be noted that here the form tag's properties contain both method and action; This is not very meaningful, because JavaScript directly manipulate the DOM, so it is possible to have no these two attributes;
Be sure to add a separate ID to the <input> tag that the user entered, which is similar to the 2nd principle. Otherwise, you cannot see the normal effect.
Step two, start adding jquery code
Let's say you've downloaded the jquery Kikuyu from the jquery official website and uploaded it to your Web server and added it to the Web page you're using.
Now another new JS file, add the following code:
Copy Code code as follows:
$ (function () {
$ (". Button"). Click (function () {
Logic for handling form validation and handing back to the background
});
});
The function () functions in the first row are the same as the Document.ready function of jquery, which is automatically triggered when the DOM is ready.
The second line contains a click Trigger function (), note that you need to place a class named "button" on the HTML page submission button to simulate the functionality of implementing the SUBMI submission form.
From the 2nd we can see that jquery can be a good separation of structure and logic.
Step three, write the validation code
In practical applications, this step is essential. When the user is missing, fill in the wrong item, prompt.
Copy Code code as follows:
$ (function () {
$ ('. Error '). Hide ();
$ (". Button"). Click (function () {
Validating code
$ ('. Error '). Hide ();
var name = $ ("Input#name"). Val ();
if (name = = "") {
$ ("Label#name_error"). Show ();
$ ("Input#name"). focus ();
return false;
}
var email = $ ("Input#email"). Val ();
if (email = = "") {
$ ("Label#email_error"). Show ();
$ ("Input#email"). focus ();
return false;
}
var phone = $ ("Input#phone"). Val ();
if (phone = = "") {
$ ("Label#phone_error"). Show ();
$ ("Input#phone"). focus ();
return false;
}
});
});
A few notes:
In line 2nd, we add a $ ('. Error '). Hide () is a label label that hides three class= "error" prompts incorrectly when the user does not enter any information. The error appears only if there is an error, which is empty. (because of the effect of return false, only one error occurs at a time)
In jquery, getting the value of an ID or class in the DOM is very simple
Copy Code code as follows:
Get the value of the ID
var name = $ ("Input#name"). Val ();
Get a value with a class ordinal of 1
var name = $ (". Name") [1].val ();
Now assume that the user does not enter a name, the processing logic should be: First display the error, and then focus on the name.
if (name = = "") {//user name is empty
$ ("Label#name_error"). Show (); Error tips
$ ("Input#name"). focus (); Focus positioning
return false; Return
}
When you validate on a required field, you must return FALSE, or you will be submitted if a required entry is not completed.
The fourth step is to submit the form information through jquery's Ajax function.
This is the core step in this tutorial to implement no refresh submissions, using AJAX functions to submit the table item values that JavaScript obtains from the DOM, and then asynchronously submits to the spooler (process.php) and sends an email. This step is immediately after the validator:
Copy Code code as follows:
var datastring = ' name= ' + name + ' &email= ' + email + ' &phone= ' + phone;
alert (datastring); return false;
$.ajax ({
Type: "POST",
URL: "bin/process.php",
Data:datastring,
Success:function () {
$ (' #contact_form '). HTML ("<div id= ' message ' ></div>");
$ (' #message '). HTML (". Append ("<p>script by Code52.net</p>")
. Hide ()
. fadeIn (1500, function () {
$ (' #message ') append (" ')";
});
}
});
return false;
The core function of the above code is. Ajax (), which is used to send the obtained form information (datastring) asynchronously to the defined background PHP URL (bin/process.php) by post. If the data is delivered successfully, it will return a series of our defined information to the user. The last return is false to prevent the page from reloading.
In addition to returning success messages and sending emails, there are other things we can do more broadly. For example, when the data is processed by a background script, the data is inserted into the database, and then the information submitted by the user is returned.
Detailed Explanation:
First, get the value of the table item, which we've already mentioned in step three:
var name = $ ("Input#name"). Val ();
var email = $ ("Input#email"). Val ();
var phone = $ ("Input#phone"). Val ();
Combine the values of a table item into a single string
var datastring = ' name= ' + name + ' &email= ' + email + ' &phone= ' + phone;
The value of this combined string is passed to the background URL through an AJAX function, and if successful, the success message is returned to the user:
Copy Code code as follows:
$.ajax ({
Type: "POST",
URL: "bin/process.php",
Data:datastring,
Success:function () {
$ (' #contact_form '). HTML ("<div id= ' message ' ></div>");
$ (' #message '). HTML (". Append ("<p>we'll be In Touch soon.</p>")
. Hide ()
. fadeIn (1500, function () {
$ (' #message ') append (" ')";
});
}
});
return false;
In this example, the AJAX function is all that's available, and if you need further information about Ajax functions, refer to the official documentation: JQuery's documentation on the AJAX function
The fifth step, the feedback information to the user part
First, after the information is submitted successfully, jquery will be dynamically replaced by the following parts of the <div id= "Contact_form" ></form> content, only a simple word can be achieved.
$ (' #contact_form '). HTML ("<div id= ' message ' ></div>");
So keep in mind that if you need to dynamically replace a layer or <span> with JavaScript later, you can use the. html method of jquery, which is very simple and convenient.
Second, with this layer is certainly not enough, because there is no content, so we have to id=message this layer to add some display content:
$ (' #message '). HTML ("It is also dynamic to add a section of HTML for the layer with ID message for hints. You can also append a sentence to the message layer using the Append method:
. Append ("<p>we'll be In Touch soon.</p>")
Finally, in order to demonstrate the dynamic effect of server processing after submission, we set the following effect code:
. Hide ()//the whole layer disappears
. fadeIn (1500, function () {//appears gradually within 1500 milliseconds
Finally, dynamically append a success icon
$ (' #message ') append (" ')";
});
PostScript: If you want to use this instance in practice, you may need to make changes to some places. For example, to increase the validation information rules, the user submitted information in the process of setting up a loading icon, and so on, this tutorial is only for the purposes of. In addition, please note that the background to submit data to the mailbox, I will not explain here. There are very detailed comments in the examples of packaged downloads, where you need to change only the username and password. After downloading the package, you may find that there is a runonload.js file in it that focuses on the input form when loading the form file.