javascript - 為什麼資料庫中顯示的是null呢?

WBOY
發布: 2016-08-18 09:15:50
原創
1072 人瀏覽過

用ajax提交以後資料庫中也多了一條 但是顯示是null 這是為什麼?我ajax用的不對嗎?

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>
登入後複製
登入後複製

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>
登入後複製
登入後複製

回覆內容:

用ajax提交以後資料庫中也多了一條 但是顯示是null 這是為什麼?我ajax用的不對嗎?

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>
登入後複製
登入後複製

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>
登入後複製
登入後複製

你好像並沒有把資料傳到後台吧。

哪裡傳值了?

send裡沒有值,有form表單了就沒必要寫ajax了把 ,配值target="iframe_name"

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!