javascript - 这个post提交方式哪里写的不对呢?
程序员文章站
2022-04-11 09:13:20
...
提交后插入不了数据 是我ajax写的不对吧?
tt.php
tt.php
ajax.php
prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?>
回复内容:
提交后插入不了数据 是我ajax写的不对吧?
tt.php
ajax.php
prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?>
1、提交按钮点击默认会触发onsubmit事件,而你给它绑定的onclick事件里没有取消默认事件;
oBtn.onclick=function(e){
var e=window.event||e;
e.preventDefault&&e.preventDefault();
e.returnValue&&e.returnValue=false;
}
2、采用默认onsubmit,无视ajax,txt1加上name="aa";
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);
}
}
}
}
ajax这个函数这样改:
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);
}
异步调用,不然数据发送不出去