javascript - 데이터베이스에 null이 표시되는 이유는 무엇입니까?

WBOY
풀어 주다: 2016-08-18 09:15:50
원래의
1071명이 탐색했습니다.

Ajax로 제출한 후 데이터베이스에 추가 항목이 있는데 표시가 null인 이유는 무엇입니까? 아약스를 잘못 사용하고 있는 걸까요?

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인 이유는 무엇입니까? 아약스를 잘못 사용하고 있는 걸까요?

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에 값이 없으면 ajax 값을 작성할 필요가 없습니다. target="iframe_name"

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!