JQuery Ajax No Refresh data submission instance

Source: Internet
Author: User
Tags php tutorial contact form

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>jquery Ajax No Refresh data submission instance </title>

<body>
<div id= "Contact_form" >
<form name= "Contact" 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 field is required.</label>

<label for= "Email" id= "Email_label" >return email</label>
<input type= "text" name= "email" id= Email "size=" value= "" class= "Text-input"/>
<label class= "error" for= "email" id= "email_error" >this field is required.</label>

<label for= "Phone" id= "Phone_label" >return phone</label>
<input type= "text" name= "phone" id= "Phone" size= "value=" "class=" Text-input "/>"
<label class= "error" for= "Phone" id= "phone_error" >this field is required.</label>

<br/>
<input type= "Submit" name= "Submit" class= "button" id= "submit_btn" value= "Send"/>
</fieldset>
</form>
</div>

You may notice that I have included an ID contact_form the div around the entire form of the wrapper.
Please be sure not to miss your own zoning form, as we will need this packing partition later. You
may also notice the part of the method I have left for the action and the Blank Form label. We
Actually don't need these people either, because jquery needs to take care of it all later.

Another important thing to be sure to include is the ID value for each input field. ID value
is the process and form your jquery script will look for.

I have added some CSS tutorial styles and background images in Photoshop tutorial to show the following forms:

2 steps-Begin adding jquery
The next step in the process is to add some jquery code. I will assume that you have
jquery downloads, uploaded to the server, and
It references on your Web page.

Next, open up another new Web effects file, reference it in your HTML, you'll be any normal JavaScript file,
and add the following content:

$ (function () {
$ (". Button"). Click (function () {
Validate and process form here
});
});

The first function () is done by the functions () that it loads the internal events as long as the HTML files are ready.
If you do any work in jquery before, functionality is as jquery as the same
Document.ready
Function. So we start from here, and there's our click function, which is submitted in a click to execute
The name of the button and the button class. Eventually we finish the work with these
Line code is the same if we want to add an OnClick event submission to the HTML button. The
So what we do with jquery is that this is a clear separation from our script demo.

Form validation

$ (function () {
$ ('. Error '). Hide ();
$ (". Button"). Click (function () {
Validate and process form here

$ ('. 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;
}

});
});


In our function to load the page when ready, we add some form validation.
But first you see the Canadian Dollar Hidden () (' wrong. ');。 What the
This is really hidden with the class name "error" of our 3 tag. We want these tags when it's not just being hidden
First load the page, and also when you click Submit case, one of the messages is to show the user
before. Each error message should only appear if validation does not work.

We first verify that if the Name field is left blank by the user, if so, then we
Display with Name_error ID label. And then we place the name in the field where the focus is on the user
At all about the next step how to do confused! (I've learned never to bear too much when it comes to forms
Users).

In order to explain in more detail how we make this happen, we set a variable name ' to the value of
Enter the field ID as "name"-all of a jquery line:

var name = $ ("Input#name"). Val ();
We then check if this is blank, and if it's, we use jquery ' s show ()
ID "Name_error":

if (name = = "") {
$ ("Label#name_error"). Show ();
}
Next, we place the form focus back on the input field with ID of ' name ', and finally return false:

if (name = = "") {
$ ("Label#name_error"). Show ();
$ ("Input#name"). focus ();
return false;
}


Now that we have reached the heart of this tutorial-our form does not submit a page refresh, it sends the form
The values in the PHP tutorial are in the background script. Let's take a look at all the code first, and then I'll break
For the next step in more detail. Validation under new fragment we previously added the following code (and before
Click the button function is turned off):

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>we'll be In Touch soon.</p>")
. Hide ()
. Fadein (1500, function () {
$ (' #message ') append (" ')";
});
}
});
return false;


Ajax

$.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 on Touch soon.</p>")
         Hide ()
        Fadein (1500, function () {
          $ (' #message '). Append (" ');
       });
     }
   });
    return false;

</body>

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.