Home > Backend Development > PHP Tutorial > Sample code of PHP value transfer method and Ajax verification function

Sample code of PHP value transfer method and Ajax verification function

黄舟
Release: 2023-03-07 06:30:02
Original
1346 people have browsed it

This article introduces to you phpThree ways to pass values ​​from the front desk to the backend for verification, as well as the verification function of ajax. Friends who need it can learn together

3 ways for PHP frontend to pass values ​​to the backend for verification

1. Nothing more than post, get and ajax methods

post and get methods are to use method methods on the form to stipulate that the main purpose is ajax dynamic value transfer for background verification

2. 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
Copy after login

/ Note that after ajax returns, regardless of whether it returns or not, the final function return is underfined/At this time, you can use the following method to verify the incoming value. You can judge first. If the returned label is a typo, assign the value. Write it into a hidden tag, and then use textContent to read whether the value we specified is written, so as to know whether the entered value is correct;

If it is correct, return true, if it is wrong, 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;
}
}
Copy after login

The above is the detailed content of Sample code of PHP value transfer method and Ajax verification function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template