gpt4 book ai didi

php - $.post 没有发布任何内容

转载 作者:行者123 更新时间:2023-11-28 16:39:13 24 4
gpt4 key购买 nike

我正在使用以下 jquery 将评论发布到 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 的监听方式如下:

$comment = htmlspecialchars($_POST["comment"]);

我可以在 Firebug 控制台中看到没有发生 POST。我仅将一个 GET 用于不同的功能(这可能是罪魁祸首吗?)

最佳答案

假设:

<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 selectors 。例如:

$("#submit_js")

将创建一个包含 ID 为 submit_js 的所有元素(应该只有零个或一个元素)的 jQuery 对象。

关于php - $.post 没有发布任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1938032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com