Home > Backend Development > PHP Tutorial > Why does TP show as undefined after submitting via ajax?

Why does TP show as undefined after submitting via ajax?

WBOY
Release: 2023-03-02 07:00:02
Original
1305 people have browsed it

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

Reply content:

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

<code>  var oName=document.getElementById('name');
   var oPw=document.getElementById('pw');
</code>
Copy after login

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