如何解决PHP:Ajax在MySQL表中复制数据
Helllo, 我正在尝试使用Ajax和PHP将表单数据发送到MySQL表。当表单字段为空并发送数据时,我确保显示一条错误消息。多次出现此错误,给我的印象是,一旦正确重新签名了所有字段,就代表表中的条目数。突然,当我查看表格时,发现表格中的数据已重复。我无法理解这。我的AJAX代码是否将数据保留在内存中,然后在正确填写数据后将其发送回php?下面,我介绍我的AJAX代码,该代码允许我发送表单数据。
function send(){
$("#form").on('submit',(function(e) {
e.preventDefault();
$("#loader").toggle();
document.getElementById("go").disabled = true;
var form_data = new FormData();
//alert(form_data);
$.ajax({
type: 'POST',url: 'send_to.php',// point to server-side PHP script
dataType: 'text',// what to expect back from the PHP script,if anything
cache: false,contentType: false,processData: false,//async: false,data: new FormData(this),success: function(data,status){
if(data == 'success'){
$("#resultat").html('<strong style="color:green;">Thank you Your transaction has been sent</strong>');
$("input[type=text]").val(""); // Vider le formulaire
$("input[type=number]").val(""); // Vider le formulaire
document.getElementById('loader').style.display = "none";
document.getElementById("go").disabled = false;
//location.reload();
//window.location.replace("./?home&yes");
}else{
$("#resultat").html('<strong style="color:red;">'+data+'</strong>');
document.getElementById("go").disabled = false;
document.getElementById('loader').style.display = "none";
}
}
});
}));
}
我的PHP代码:
$sql = $mysqli->prepare("INSERT INTO test (user,phone,email) VALUES (?,?,?)");
$sql->bind_param('sis',$user,$phone,$email);
if(!$sql->execute()){
echo ('cant create);
}else{
echo 'data created';
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。