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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题