After submission, the background receives data through $name=I('name') and $pw=I('pw'), but after the echo comes out, it shows undefined. Why is this? The returned value is always 0. Is it the ajax part of the code? But it’s correct when you check it with F12
<code><script type="text/javascript"> function ajax(url,data,data2,funsucc){ var oAjax=new XMLHttpRequest(); oAjax.open('post',url,true); oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); oAjax.send("name="+data+"&pw="+data2); oAjax.onreadystatechange=function(){ if(oAjax.readyState==4){ if(oAjax.status==200){ funsucc(oAjax.responseText); } } } } </script> <script type="text/javascript"> window.onload=function(){ var oBtn1=document.getElementById('btn1'); var oName=document.getElementById('name'); var oPw=document.getElementById('pw'); var oDiv5=document.getElementById('div5'); oBtn1.onclick=function(){ oV1=oName.value; oV2=oPw.value; ajax("{:U('login/ajax')}",oV1,oV2,function(str){ oDiv5.innerHTML=str; }); } } </script></code>
<code>public function ajax(){ $user=M('user'); $name=I('name'); $pw=I('pw'); echo $pw; //提示未定义 echo $name; //提示未定义 $res1=$user->where("username='$name' AND password='$pw'")->select(); if($res1){ $res=1; }else{ $res=0; } $this->ajaxReturn($res); }</code>
After submission, the background receives data through $name=I('name') and $pw=I('pw'), but when the echo comes out, it shows undefined. Why is this? The returned value is always 0. Is it the ajax part of the code? But it’s correct when you check it with F12
<code><script type="text/javascript"> function ajax(url,data,data2,funsucc){ var oAjax=new XMLHttpRequest(); oAjax.open('post',url,true); oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); oAjax.send("name="+data+"&pw="+data2); oAjax.onreadystatechange=function(){ if(oAjax.readyState==4){ if(oAjax.status==200){ funsucc(oAjax.responseText); } } } } </script> <script type="text/javascript"> window.onload=function(){ var oBtn1=document.getElementById('btn1'); var oName=document.getElementById('name'); var oPw=document.getElementById('pw'); var oDiv5=document.getElementById('div5'); oBtn1.onclick=function(){ oV1=oName.value; oV2=oPw.value; ajax("{:U('login/ajax')}",oV1,oV2,function(str){ oDiv5.innerHTML=str; }); } } </script></code>
<code>public function ajax(){ $user=M('user'); $name=I('name'); $pw=I('pw'); echo $pw; //提示未定义 echo $name; //提示未定义 $res1=$user->where("username='$name' AND password='$pw'")->select(); if($res1){ $res=1; }else{ $res=0; } $this->ajaxReturn($res); }</code>
<code> var oName=document.getElementById('name'); var oPw=document.getElementById('pw'); </code>
Try out these two data and take a look. If no object assignment is found, it will be undefined;
undefined must be a js problem, please post the detailed error
Check the data in the submission form
ajax("{:U('login/ajax')}",oV1,oV2,function(str){ });
<code> 你这个地方只是把值传进去了,没有定义 name,pw.</code>