如何用Javascript从submit.php获得结果
How to get the result from submit.php with Javascript?
你好,我有这些HTML表单:
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="./comments.js"></script>
</head>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>
这是我的JS:
$(document).ready(function () {
// $(".add-comment-submit") selects all elements with the class "add-comment-submit", so the "submit" buttons
// .on will handle event binding for the click event on any of those buttons
$(".add-comment-submit").on("click", function (e) {
e.preventDefault();
// 'this' is the element that fired the event, so the submit button
var submit = $(this);
// check if the submit button has the "working" class added. this will tell us that someone already clicked it
// and we shouldn't continue
if (submit.hasClass("working")) return false;
// add "working" class to the submit button to prevent double click
submit.addClass("working");
submit.val("Working...");
/* Sending the form fileds to submit.php: */
// do the post, and on callback (simulated with setTimeout) set everythign back
$.post('submit.php',$(this).serialize(),function(msg){
// simulate post
setTimeout(function () {
submit.removeClass("working");
submit.val("Submit");
//$(msg.html).hide().insertBefore(submit.parent()).slideDown();
// simulate getting results back from server
$("<div>Here are some results or something</div>").hide().insertBefore(submit.parent($(".addCommentContainer"))).slideDown();
}, 1000);
});
});
});
在这个JS中,在按下SUBMIT按钮后,每个表单都会出现"Here are some results or something"的消息。
<?PHP
$message = "Hello world";
echo $message;
?>
我的问题是-我如何使JS打印$message
提交而不是"这里有一些结果或一些东西"?
你能告诉我我要改变我的剧本,所以它会工作吗?提前感谢!
文档明确指出,回调中的第一个参数是来自服务器的响应。
$.post('submit.php',$(this).serialize(),function(msg){
// simulate post
setTimeout(function () {
submit.removeClass("working");
submit.val("Submit");
//$(msg.html).hide().insertBefore(submit.parent()).slideDown();
// simulate getting results back from server
$("<div>" + msg +"</div>").hide().insertBefore(submit.parent($(".addCommentContainer"))).slideDown();
}, 1000);
});
相关文章:
- 将地理编码结果转换为php变量以发布到mysql数据库
- 使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe
- AJAX调用后Php结果未显示
- Ajax/Php得到一个以上的结果
- 使用PHP通过HTML表单选项选择器过滤MYSQL结果
- PHP,MySQL,AJAX-调用AJAX结果后,Bootstrap CSS样式不会显示
- 如何根据JavaScript函数的结果用PHP编写数据库?(FB应用程序)
- 防止 PHP 在新页面中显示结果
- Javascript调用php函数并接收返回的结果
- 在php中隐藏/显示切换回声结果
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- 如何以绘图表格式输出PHP结果
- 使用ajax运行php脚本不会显示任何结果
- php中的日期格式给出了错误的结果
- 如何使用SHA256使用PHP和CryptoJS获得相同的结果
- php/mysql搜索结果-当类型>1,同时保持其他类型不>1-代码点火器
- 如何使用ajax调用php并返回结果
- 如何在php JSON编码结果中包含谷歌分析
- 如何在javascript变量中获取php文件的结果
- 在jQuery函数中获取结果PHP代码