php - jquery ajax 上的 Textarea 没有发送到 mysql,只是输入字段

标签 php jquery mysql ajax textarea

我在尝试将 textarea 的值发送到 mysql 时遇到问题。 输入postusername的值发送成功,但textarea的值没有发送。

当它工作时(我不知道为什么它停止了),第一次发送了值​​,但是如果我尝试在不刷新页面的情况下发送另一个值,则再次发送之前的值而不是新值插入文本区域。

现在,textarea 在警报窗口中返回一个空值,并且只在 mysql 中记录输入 postusername

<form id="newpostform" method="post" style="width:90%;margin-left:auto;margin-right:auto;margin-top:-35px;margin-bottom:10px;font-family:Calibri,Arial;">
    <textarea class="editbook" name="newpostcontent"></textarea>
    <div style="margin-top:10px;margin-right:25px;">
        <input type="text" value="<?php echo $username0; ?>" name="postusername">
        <input class="Button" style="float:right;margin-bottom:10px;margin-left:10px;" type="button" value="Preview">
        <input class="Button" style="float:right;margin-bottom:10px;margin-left:10px;" type="button" value="Save draft">
        <input id="sendButton" class="Button" style="float:right;margin-bottom:10px;" type="button" value="Send">
    </div>
</form>
<script>
    $(function () {
        $("input#sendButton").on("click", function () {
            $.post('newpost.php', $("form#newpostform").serialize(), function (data) {
                alert($("textarea[name=newpostcontent]").val());
            });
        });
    });
</script>

newpost.php

require("../db_info.php");
mysql_query("
    INSERT INTO
        post (
            author, 
            content
        ) 
        VALUES (
            '".$_POST['postusername']."', 
            '".$_POST['newpostcontent']."'
    )"
);

如果我这样做,例如:

mysql_query("
    INSERT INTO
        post (
            author, 
            content
        ) 
        VALUES (
            '".$_POST['postusername']."', 
            'Why doesn't this work?'
    )"
);

$_POST['postusername'] 和短语“Why doesn't this work?”正确写在sql中。

感谢您的关注。

最佳答案

这样试试

var content=$("textarea[name=newpostcontent]").val(),
username=$("input[name=postusername]").val();
$.post('newpost.php', {newpostcontent:content,postusername:username}, function (data) {
     alert(content);
});

希望这能行得通

关于php - jquery ajax 上的 Textarea 没有发送到 mysql,只是输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485687/

相关文章:

php - 如何在mysql中添加textarea内容?

php - 登录页面中出现错误 symfony - 警告 : gettext()

PHP 如何在 2 列中显示卡片?

javascript - 将变量内容保存到文件

javascript - Jquery - 获取点击的DIV元素的ID

mysql - 根据匹配订单id从orderdetails表中获取product_id的数量

javascript - Sequelize 自引用 hasMany 不创建别名函数

php - 如何获取MVC中最后一次插入的ID?

javascript - 自动计算天数并填充月、年

javascript - 根据给定日期突出显示日期单元格