Home > Article > Backend Development > javascript - What is wrong with this post submission method?
The data cannot be inserted after submission. Is it because I wrote it in ajax?
tt.php
ajax.php
prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?>
The data cannot be inserted after submission. Is it because I wrote it in ajax?
tt.php
ajax.php
prepare("insert into ajax(txt)values(?)");
$stmt->execute(array($txt));
?>
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;
oBtn.onclick=function(e){
var e=window.event||e;
e.preventDefault&&e.preventDefault();
e.returnValue&&e.returnValue=false;
}
2. Use the default onsubmit, ignore ajax, add name="aa" to txt1;
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);
}
}
}
}
The ajax function is modified like this:
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);
}
Asynchronous call, otherwise the data cannot be sent