Home>Article>Backend Development> PHP value transfer method and Ajax method to implement verification function
This article introduces you to three ways of passing values from the PHP frontend to the backend for verification, as well as the verification function of ajax. Friends who need it can learn together.
PHP frontend transfer 3 ways to verify values in the background
1. Nothing more than post, get and ajax
The post and get methods are The method method used on the form is mainly to use ajax dynamic value transfer for background verification
2. The ajax dynamic value transfer code is as follows
//给调用ajax的这里设一个函数 function u_ajax(uname,upass){ //创建ajax if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveObject(“Microsoft.XMLHTTP”); } //打开一个页面 xmlhttp.open(“post”,”../dao/loginAction.php”,true); //在用post方式传值的时候要加上这句,不然后台会收不到 xmlhttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”); //这里传值过去 xmlhttp.send(“uname=”+uname+”&&”+”upassword=”+upass); //准备返回处理 xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200 ){ //这里是返回的值 var res = xmlhttp.responseText; //返回处理 if(res == "errn"){ document.getElementById("err").style.visibility="visible"; document.getElementById("err").style.color="red"; document.getElementById("err").innerHTML="账号或密码错误!"; return false; }else{ //若无返回值则定为空 document.getElementById("err").innerHTML=""; } } } }//ajax
/Note that after ajax returns, regardless of whether it is returned or not, the final function return is underfined/At this time, you can use the following method to verify the incoming value. You can first judge if the returned value is a typo The tag is responsible for writing the value into a hidden tag, and then using textContent to read whether the value we specified is written, so as to know whether the input value verification is correct;
If it is correct, return true , if there is an error, return false;
Since ajax does not return any value, it is all underfined, so we need to use another separate function to call the ajax function, and we cannot return the function
//调用上面的函数,并通过上面生成的值进行判断 function lg_verity(){ //调用上面的函数 ,这里返回了,那里面ajax部分就不有用了 my_lg_verity(); //得到内容的值 var errText = document.getElementById("err").textContent; //这里用来判断是否有写入值,判断是否可以跳转的 if(errText.length>0){ return false; } }
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Ajax and mysql data interaction productionMessage boardFunction
php implements theMessage Boardfunction based on session control
PHP method to implement the simpleMessage Boardfunction
The above is the detailed content of PHP value transfer method and Ajax method to implement verification function. For more information, please follow other related articles on the PHP Chinese website!