1

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.

3
  • Even if there is a parse error in your newpost.php the alert should have worked. The above code seems to work fine for me. Try to see the ajax request via firebug. Commented Dec 10, 2013 at 5:09
  • I forgot to say, I'm using TinyMCE. Without it, the script works great. But I need a text editor with html editor for my site. Someone recommended me one? Commented Dec 10, 2013 at 17:21
  • The problem was solved with this answer and with tinyMCE.triggerSave(); before the $.post and variables. Thanks for the help. Commented Dec 10, 2013 at 17:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This only works if I refresh page after enter a value on the textarea. But the site need send the value (most than one time and different values) without refresh the page. Seems that the ajax is getting the initial value of textarea, that is empty. My page isn't updating the textarea's value according to the user writing.
I solved the problem with tinyMCE.triggerSave(); before of the variables that you created. Now the TinyMCE and Ajax works great! Thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.