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 )}}}