Home > Backend Development > PHP Tutorial > javascript - What is wrong with this post submission method?

javascript - What is wrong with this post submission method?

WBOY
Release: 2016-08-18 09:15:49
Original
1274 people have browsed it

The data cannot be inserted after submission. Is it because I wrote it in 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>
Copy after login
Copy after login

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

Reply content:

The data cannot be inserted after submission. Is it because I wrote it in 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>
Copy after login
Copy after login

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

1. Clicking the submit button will trigger the onsubmit event by default, but the onclick event you bound to it does not cancel the default event;

<code>oBtn.onclick=function(e){
    var e=window.event||e;
    e.preventDefault&&e.preventDefault();
    e.returnValue&&e.returnValue=false;
}</code>
Copy after login

2. Use the default onsubmit, ignore ajax, add name="aa" to txt1;

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

The ajax function is modified like this:

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

Asynchronous call, otherwise the data cannot be sent

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