Form submission solution Summary: Form submission Solution

Source: Internet
Author: User

Form submission solution Summary: Form submission Solution
1. Method 1: Form submission: In this method, you need to pin the action attribute of the form and change the type attribute of the submit button to submit. <Form name = "userForm" target = "_ self" id = "userForm" action = "#" method = "post">
<! -- Start user information --> <input name = "userName" type = "text" class = "text1" id = "userName" size = "20" maxlength = "20"> <input name = "password" type = "password" class = "text1" id = "password" size = "20" maxlength = "20"> <! -- End user information -->
<Div align = "center"> <! -- Submit button -->
<Input name = "btnAdd" class = "button1" type = "submit" id = "btnAdd" value = "add" onClick = "addUser ()"> </div>

</Form>


2. Method 2: submit a form using JS, trigger a JS event using the button, and obtain the Form ID for submission. (1) The form information is as follows:
<Form name = "userForm" target = "_ self" id = "userForm">
<! -- Start user information -->
<Input name = "userName" type = "text" class = "text1" id = "userName" size = "20" maxlength = "20"> <input name = "password" type = "password" class = "text1" id = "password" size = "20" maxlength = "20"> <! -- End user information -->

<Div align = "center"> <! -- Submit button -->
<Input name = "btnAdd" class = "button1" type = "button" id = "btnAdd" value = "add" onClick = "addUser ()"> </div>

</Form>


2) The JS submission code is as follows:
Function addUser (){
With (document. getElementById ("userForm ")){
Action = "user_add.jsp"; method = "post"; method = "post ";}

}


3. Method 3: Use ajax for submission. (The submission event is triggered when the text box loses focus.) The following 1) HTML code: <input name = "userId" type = "text" class = "text1" id = "userId" size = "10" maxlength = "10" onkeypress = "userIdOnkeypress () "value =" <% = userId %> "onblur =" validate (this) "> <span id =" spanUserId "> </span>

2) JS Verification Code
Var xmlHttp;
Function createXMLHttpRequest () {// indicates that the current browser is not ie, such as ns, firefox if (window. XMLHttpRequest) {xmlHttp = new XMLHttpRequest ();} else if (window. activeXObject) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}} function validate (field) {// alert (field. value); if (trim (field. value ). length! = 0) {// create the AJAX core object XMLHttpRequest createXMLHttpRequest (); var url = "user_validate.jsp? UserId = "+ trim (field. value) + "& time =" + new Date (). getTime (); // set the request method to Get, set the request URL, and set it to submit xmlHttp asynchronously. open ("GET", url, true); // copy the method address to the onreadystatechange attribute xmlHttp. onreadystatechange = callback; // send the request information to the Ajax engine xmlHttp. send (null);} else {document. getElementById ("spanUserId "). innerHTML = "" ;}} function callback () {// if (xmlHttp. readyState = 4) {// the HTTP protocol status is successful if (xmlHttp. status = 200) {if (tri M (xmlHttp. responseText )! = "") {Document. getElementById ("spanUserId "). innerHTML = "<font color = 'red'>" + xmlHttp. responseText + "</font>";} else {document. getElementById ("spanUserId "). innerHTML = "" ;}} else {alert ("request failed, error code =" + xmlHttp. status )}}}

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.