javascript - 这个post提交方式哪里写的不对呢?

php中文网
发布: 2016-08-18 09:15:49
原创
1430人浏览过

提交后插入不了数据 是我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事件里没有取消默认事件;

立即学习Java免费学习笔记(深入)”;

Devv
Devv

Devv是一个专为程序员打造的新一代AI搜索引擎

Devv 140
查看详情 Devv
<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>
登录后复制

异步调用,不然数据发送不出去

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号