dongpin6941 2013-12-10 03:49
浏览 67
已采纳

关于jquery ajax的Textarea不是发送到mysql,只是输入字段

I'm having problems trying to send the value of a textarea to mysql. The value of the input postusername is successfully sent, but not the value of the textarea.

And when it was working (I don't know why it stopped), the value was sent in the first time, but if I tried to send another value without refreshing the page, the previous value was sent again and not the new value inserted into textarea.

Now, the textarea is returning me an empty value in alert window and is recording only the input postusername in mysql.

<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']."'
    )"
);

If I do, for example:

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

The $_POST['postusername'] and the phrase "Why doesn't this work?" is written in the sql correctly.

Thanks for your attention.

  • 写回答

1条回答 默认 最新

  • dongping1689 2013-12-10 06:33
    关注

    try like this

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

    hope this works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?