Submit a form using Ajax

Source: Internet
Author: User

1. ajaxform

Purpose:

(1) pre-load JS events to form

(2) Submit the JS verification form before the action

(3) After submitting the action, the result will be processed by JS and will not be automatically redirected.

Requirements:

<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script><script type="text/javascript" src="js/jquery.form.js"></script>

Example:

<SCRIPT type = "text/JavaScript"> // var error = document. getelementbyid ('errordiv '); // note that VaR error is declared only here. Do not assign values. The value assignment is also null var error; // wait for the Dom to be loaded $ (document ). ready (function () {error = document. getelementbyid ('errordiv '); // bind 'myform' and provide a simple callback function var Options = {// target:' # output1 ', // target element (s) to be updated with server response beforesubmit: validate, // pre-submit callback datatype: 'json', // 'xml', 'script ', or 'json' (expected server response type) // datatype must be placed before callback (JSON), because callback (JSON) needs to call the JSON variable success: function (JSON) {JSON = eval (JSON); If (JSON. errcode = 0) {window. location. href = 'seller/seller. JSP ';} else {error. innerhtml = "incorrect account and password. Please log on again. ";}}// post-submit callback // other available options: // URL: URL // override for form's 'action' attribute/type: type // 'get' or 'post', override for form's 'method' attribute // clearform: True // clear all form fields after successful submit // resetform: true // reset the form after successful submit // $. ajax options can be used here too, for example: // Timeout: 3000}; $ ('# loginform '). ajaxform (options) ;}); function validate () {var username = document. getelementbyid ("name "). value; var Password = document. getelementbyid ("PWD "). value; // if it is null, it can be handed over to the foreground for processing; If (username. length = 0) {error. innerhtml = "account cannot be blank"; return false;} else if (password. length = 0) {error. innerhtml = "the password cannot be blank"; return false;} error. innerhtml = "logging on" ;}</SCRIPT>

 

 

Done

Submit a form using Ajax

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.