This article introduces three methods for passing values from the php foreground to the background for verification, as well as the ajax verification function, if you need to learn it together, this article introduces three methods for passing values to the backend for verification at the php front-end, as well as the ajax verification function.
Three methods for PHP foreground value passing to the background for verification
1. post, get, and ajax
The post and get methods are defined by the method on the form. The main reason is that ajax dynamically transmits values for background verification.
2. the ajax dynamic value passing code is as follows:
// Set a function u_ajax (uname, upass) {// create ajax if (window. XMLHttpRequest) {xmlhttp = new XMLHttpRequest ();} else {xmlhttp = new ActiveObject ("Microsoft. XMLHTTP ");} // open a page xmlhttp. open ("post ",".. /dao/loginAction. php ", true); // This sentence should be added when the post method is used to pass the value, otherwise the background will not receive xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); // The value is passed to xmlhttp. send ("uname =" + uname + "&" + "upassword =" + upass ); // Prepare to return and process xmlhttp. onreadystatechange = function () {if (xmlhttp. readyState = 4 & xmlhttp. status = 200) {// the returned value is var res = xmlhttp. responseText; // response processing if (res = "errn") {document. getElementById ("err "). style. visibility = "visible"; document. getElementById ("err "). style. color = "red"; document. getElementById ("err "). innerHTML = "incorrect account or password! "; Return false;} else {// if no value is returned, it is set to null document. getElementById (" err "). innerHTML =" ";}}}// ajax
/NOTE: After ajax is returned, whether it is returned or not, the final function returns underfined/. in this case, you can use the following method to verify the passed value. you can first judge, if the returned tag is an error code, write the value into a hidden tag and use textContent to read whether the specified value is written to the tag to check whether the entered value is correct;
Return true if it is correct, and false if it is incorrect;
Since ajax does not return any value, it is underfined, so we need to use another independent function to call the ajax function.
// Call the above function and use the value generated above to Judge function lg_verity () {// call the above function, and return here, in the ajax section, my_lg_verity () is not used; // you can obtain the content value var errText = document. getElementById ("err "). textContent; // used to determine whether a write value exists and whether the if (errText. length> 0) {return false ;}}
The above is the details of the sample code for the php value passing method and ajax verification function. For more information, see other related articles in the first PHP community!