1

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.

C1pher
  • 1,933
  • 6
  • 33
  • 52
Tommy
  • 667
  • 1
  • 10
  • 25

1 Answers1

4

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();

Community
  • 1
  • 1
Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23
  • @Tommy, in that case, here's your answer. :) – Maxim Kumpan Jul 08 '13 at 20:35
  • @Tommy, No, right before `var poster = $.post('addnews.php', $(this).serialize());`. You see, `$("#form").submit(function(event)` is a trigger. When you reach triggerSave(), it executes immediately after being read, and anything inside `$("#form").submit` - only after submitting the form. – Maxim Kumpan Jul 09 '13 at 06:41
  • Don't mention it, glad to be of help. – Maxim Kumpan Jul 09 '13 at 06:46