Tommy
Tommy

Reputation: 697

jQuery wont post textarea?

I have a problem with my code it won't send the textarea in the addnews.php

When I use <input type="text" name="content">, then it sends but with <textarea> it won't send.

jQuery

$("#form").submit(function(event) {
    var form = this;
    event.preventDefault();
    var poster = $.post('addnews.php', $(this).serialize());
    poster.done(function(data) {
        $(".result").html(data);
        setTimeout(function() { $(".result").hide(); }, 2000);
        form.reset();
    });
});

PHP

if (isset($_POST['rubric'])) { 
    if (empty($_POST['rubric']) && empty($_POST['content'])) { 
        echo '<div class="alert alert-error alert-box">Du måste fylla i alla fält.</div>'; 
    }
    else { 
        $rubric = htmlentities($_POST['rubric']);
        $sql = "INSERT INTO ".PREFIX."news(`date`, `poster`)
        VALUES('".time()."', '".$userID."')"; 
        mysql_query($sql);
        $id = mysql_insert_id();
        $sql2 = "INSERT INTO ".PREFIX."news_contents(`newsID`,  `headline`, `content`)
        VALUES('".$id."', '".$rubric."', '".$_POST['content']."')";
        mysql_query($sql2);
        echo '<div class="alert alert-box alert-success">Klart, nyheten postad.</div>'; 
    }
}

HTML

echo '<div class="result"></div>';
echo '<form method="post" id="form" style="padding: 5px;">';
    echo '<input type="text" name="rubric" placeholder="Rubrik..." style="width: 778px;
                border-bottom: none;font-size: 10px;
                font-family:Verdana, Arial, Helvetica, sans-serif;"><br>';
    echo '<textarea name="content" class="myTextEditor" style="width: 800px; 
                height: 500px;"></textarea>'; 
    echo '<br><input type="submit" name="addnews" value="Spara" 
                class="btn btn-primary btn-small pull-right" style="font-weight:bold;">
                <a href="?p=news" class="btn btn-mini addbutton pull-right" 
                style="font-weight:bold;margin-right: 10px;">Tillbaka</a>';
echo '</form>';

In the PHP file, I did a print_r($_POST) and it sends the content, but it is empty even if I write something.

Upvotes: 1

Views: 1347

Answers (1)

Maxim Kumpan
Maxim Kumpan

Reputation: 2625

I suspect I know what's going on.

Your textarea has the class myTextEditor. Do you happen to have some kind of js rich text editor hanging from that class?

If you do, note that not all text editors modify the morphed textarea directly. Some (like CKEditor) have a special method that updates the textarea with new text. This problem does not show up when you submit the form - only when you try to serialize it or manipulate it via JS.

Check the editor docs (or provide a link to the editor - 'myTextEditor' as a search term is incredibly ambiguous) if this is the case.

Update:

Since you are using TinyMCE, the answer to your question lies here.

In brief:

before submit call tinyMCE.triggerSave();

Upvotes: 4

Related Questions