javascript - Why is null displayed in the database?

WBOY
Release: 2016-08-18 09:15:50
Original
1072 people have browsed it

After submitting with ajax, there is an additional entry in the database, but the display is null. Why is this? Am I using ajax incorrectly?

t.php

<code><script type="text/javascript">
        function ajax(url,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);
            oAjax.send();    
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
              }
            }
        }
    </script>
<script type="text/javascript">
        window.onload=function(){
            var oTxt=document.getElementById('txt1');
            var oBtn=document.getElementById('btn1');
            oBtn.onclick=function(){
                ajax("ajax.php",function(){
                    window.location.reload();
                });
            }
        }
    </script>
<form method="post">
    <input type="text" id="txt1" name="txt">
    <button id="btn1" type="submit">提交</button>
</form></code>
Copy after login
Copy after login

ajax.php

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$stmt=$pdo->prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?></code>
Copy after login
Copy after login

Reply content:

After submitting with ajax, there is an additional entry in the database, but the display is null. Why is this? Am I using ajax incorrectly?

t.php

<code><script type="text/javascript">
        function ajax(url,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);
            oAjax.send();    
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
              }
            }
        }
    </script>
<script type="text/javascript">
        window.onload=function(){
            var oTxt=document.getElementById('txt1');
            var oBtn=document.getElementById('btn1');
            oBtn.onclick=function(){
                ajax("ajax.php",function(){
                    window.location.reload();
                });
            }
        }
    </script>
<form method="post">
    <input type="text" id="txt1" name="txt">
    <button id="btn1" type="submit">提交</button>
</form></code>
Copy after login
Copy after login

ajax.php

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$stmt=$pdo->prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?></code>
Copy after login
Copy after login

It seems that you did not transfer the data to the background.

Where is the value passed?

There is no value in send. If there is a form, there is no need to write ajax. The value target="iframe_name"

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!