$ .post不发布任何东西 [英] $.post not POSTing anything
问题描述
我正在使用以下jquery将评论发布到user_submit.php
i am using the following jquery to post comments to user_submit.php
var submit = document.getElementById("submit_js");
var comment = document.getElementById("comment");
$("#submit").bind('click', function(){
$.post("user_submit.php", {
comment: $("#comment").text()
});
});
user_submit.php的监听方式如下:
the user_submit.php listens like:
$comment = htmlspecialchars($_POST["comment"]);
我可以在Firebug控制台中看到没有POST发生.我只有一个用于其他功能的GET(这可能是罪魁祸首吗?)
i can see in the Firebug console that there is no POST happening. only a single GET that i use for a different function (can this be the culprit?)
推荐答案
假设:
<input type="text" id="comment">
<input type="button" id="submit_js">
您想要的:
$(function() {
$("#submit_js").click(function() {
$.post("user_submit.php", {
comment: $("#comment").val()
});
});
});
PHP:
<?php
$comment = $_POST['comment'];
//...
echo htmlspecialchars($comment); // nothing catches this currently
?>
您似乎还在代码中混淆了"submit"和"submit_js".我也建议不要不必要地混合Javascript和jQuery代码("getElementById()"业务).您应该熟悉 jQuery选择器.例如:
You also seem to be confusing "submit" and "submit_js" in your code. I'd advise against mixing Javascript and jQuery code unnecessarily too (the "getElementById()" business). You should familiarize yourself with jQuery selectors. For example:
$("#submit_js")
将使用ID为submit_js
的所有元素(应该只能为零或一个元素)创建一个jQuery对象.
will create a jQuery object with all the elements (which should only be zero or one elements) with the ID of submit_js
.
这篇关于$ .post不发布任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!