rafaelfuentes
rafaelfuentes

Reputation: 13

Textarea on jquery ajax isn't sending to mysql, just input field

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.

Upvotes: 1

Views: 816

Answers (1)

Ashok Maharjan
Ashok Maharjan

Reputation: 163

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

Upvotes: 1

Related Questions