提交後插入不了資料 是我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>
異步調用,不然資料發送不出去