![](/img/trans.png)
[英]php .session_start show errors on contact.php from 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.php:
<?PHP
$message = "Hello world";
echo $message;
?>
我的问题是-我如何使JS在$message
Submit上打印$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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.