javascript - 이 게시물 제출 방법에 어떤 문제가 있나요?

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

제출 후 데이터를 삽입할 수 없습니다. Ajax로 작성했기 때문인가요?
tt.php

<code><!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function ajax(url,data,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            oAjax.send("aa="+data);    
            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",oTxt.value,function(){
                    window.location.reload();
                });
            }
        }
    </script>
</head>
<body>
<form method="post">
    <input type="text" id="txt1">
    <button id="btn1" type="submit">提交</button>
</form>
</body>
</html></code>
로그인 후 복사
로그인 후 복사

ajax.php

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["aa"];      
$stmt=$pdo->prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?></code>
로그인 후 복사
로그인 후 복사

답글 내용:

제출 후 데이터를 삽입할 수 없습니다. Ajax로 작성했기 때문인가요?
tt.php

<code><!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function ajax(url,data,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            oAjax.send("aa="+data);    
            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",oTxt.value,function(){
                    window.location.reload();
                });
            }
        }
    </script>
</head>
<body>
<form method="post">
    <input type="text" id="txt1">
    <button id="btn1" type="submit">提交</button>
</form>
</body>
</html></code>
로그인 후 복사
로그인 후 복사

ajax.php

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["aa"];      
$stmt=$pdo->prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?></code>
로그인 후 복사
로그인 후 복사

1. 제출 버튼을 클릭하면 기본적으로 onsubmit 이벤트가 실행되지만, 여기에 바인딩된 onclick 이벤트는 기본 이벤트를 취소하지 않습니다.

<code>oBtn.onclick=function(e){
    var e=window.event||e;
    e.preventDefault&&e.preventDefault();
    e.returnValue&&e.returnValue=false;
}</code>
로그인 후 복사
2. 기본 onsubmit을 사용하고 ajax를 무시하고 txt1에 name="aa"를 추가합니다.

<code>function ajax(url,data,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            oAjax.send("aa="+data);//在这里打个断点看看
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
             }
           }
        }</code>
로그인 후 복사
다음과 같이 ajax 기능을 변경하세요.

비동기 호출, 그렇지 않으면 데이터를 보낼 수 없습니다
<code>function ajax(url,data,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('POST',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");          
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
             }
           }
              oAjax.send("aa="+data);   
        }</code>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!