提交后插入不了数据 是我ajax写的不对吧?
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); }
异步调用,不然数据发送不出去