我的问题是,当使用ajax提交和保存数据时,它会给出响应文本,之后它会再次回显整个表单。
示例:
this is a form
submit button save button
When response received from ajax the page looks like this..
this is a form
ajax response..
this form again
submit button save button
submit button save button
调用此按钮时
<div id="output"></div>
<button type="button" onClick="save()">Save to drafts</button>
它调用ajax函数来执行
function save(){
var save = new XMLHttpRequest;
var url = "panel.php";
var name = document.getElementById("name").value;
var page = document.getElementById("page").value;
var cont = document.getElementById("cont").value;
var send = "name="+name+"&page="+page+"&cont="+cont+"&call=yes";
save.open("POST", url, true);
save.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
save.onreadystatechange = function(){
if(save.readyState == 4 && save.status == 200){
var output = save.responseText;
document.getElementById("output").innerHTML = output;
}
}
save.send(send);
document.getElementById("output").innerHTML = "saving.... Please wait..";
}
我的php脚本只是回显已保存或未保存的字符串。
答案 0 :(得分:1)
我猜panel.php
也是首先包含该表单的页面。
确保在输出您的AJAX响应后die()
,否则它将继续愉快地输出页面上的任何其他内容 - 在这种情况下,您的表单。