【发布时间】:2016-03-14 05:11:45
【问题描述】:
这是我第一次使用javascript,请尊重。我有一个通过 ajax 提交数据的表单。一切都按预期工作,但是我试图将 recd.php 回显的内容分配给 reresponse,以便在警报中显示正确的错误代码。任何帮助或示例将不胜感激。
表格:
<form action="recd.php" method="post" id="GAMEAPPID">
<input type="text" name="GAMEAPPID" id="GAMEAPPID" />
<input type="submit">
</form>
Javascript:
<script>
$(function(){
$("#GAMEAPPID").on("submit", function(e){
// prevent native form submission here
e.preventDefault();
// now do whatever you want here
$.ajax({
type: $(this).attr("method"), // <-- get method of form
url: $(this).attr("action"), // <-- get action of form
data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
$("#result").html("");
},
success: function(data){
$("#result").html(data);
if(recresponse === "0") {
alert("Incomplete.");
}
if(recresponse === "1") {
alert("Duplicate.");
}
if(recresponse === "2") {
alert("Failed");
}
if(recresponse === "3") {
alert("Thanks");
}
document.getElementById("GAMEAPPID").reset();
refreshMyDiv();
}
});
});
});
</script>
【问题讨论】:
-
recresponse是什么?它在任何地方都有定义吗? -
recresponse 是由 recd.php 回显的数字,例如在提交时未定义:recd.php echos 0 1 2 3
-
重命名对数据的响应,其次不要在表单和元素中使用相同的 ID(不好的做法)
-
让我试试谢谢
-
响应作为
success处理程序的参数接收。在您的情况下,data是来自服务器的响应..
标签: javascript php jquery ajax