1

The textarea is not reading any input that is typed into the box. Initially, I was using PHP to check if the textarea was empty, and was recieveing an error there. So I removed that check, to see if it was php that was causing the issue, and added the required="required" attribute to the textarea tag, and even that is coming back with Please fill out this field. I am not quite sure where I am going wrong with my code, I had it working previously, then all of a sudden it stopped working, and I am completely confused as to why. I also looked at various other posts about the textarea not submitting, and ensured that I was checking the post with the name, not the ID; and making sure the textarea was submitting to the same form as the submit button. I have also tried it without specifying the form on the textarea tag.

HTML Code:

    <form action="" method="post" id="CreateTopicForm">
        <input type="hidden" name="create-topic" />
        <span class="secondary radius label"><strong>Title</strong></span>
        <input type="text" name="title" id="title" />

        <span class="secondary radius label"><strong>Message</strong></span>
        <textarea name="content" id="content" required="required" form="CreateTopicForm"></textarea>

        <?php if($_SESSION['user']['account_type'] >= 3): ?>
            <span class="secondary radius label"><strong>Sticky Topic</strong></span>
            <input type="checkbox" name="sticky" /><br />
        <?php endif ?>

        <input type="submit" value="Post Topic" class="topic-post" />
    </form>

PHP Code:

/* Retrieve necessary variables */

$fid = $_GET['fid'];

/* Get Forum Information */
$query = "SELECT * FROM bkg_forums where forum_id = :id";
$query_params = array(
':id' => $fid
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
} catch(PDOException $e) {
$error[] = $pdoerror;
}
$forum = $stmt->fetchAll();

/* Begin the database upload */

if(!empty($_POST)){ /* Plan to change to if($_REQUEST['submit']) */
/* Check if data was actually submitted */



$db->beginTransaction();

/* DO SOME ERROR CHECKING. MAKE SURE FIELDS ARE NOT EMPTY. */
    if(empty($_POST['title'])){
        $error[] = "Sorry! You must enter a title!";
    }
    /* Previously had a check if $_POST['content'] */

/* GENERATE SOME VARIABLES NEEDED TO INSERT INTO TABLES. ACCOUNT_TYPE IS TEMPORARY*/
    if($_SESSION['user']['account_type'] == 0) {
        $account_type = "Normal";
        $color = "white";
    } elseif($_SESSION['user']['account_type'] == 1) {
        $account_type = "Donator";
        $color = "#F4FA58";
    } elseif($_SESSION['user']['account_type'] == 2) {
        $account_type = "Moderator";
        $color = "#2EFE2E";
    } elseif($_SESSION['user']['account_type'] == 3) {
        $account_type = "Community Manager";
        $color = "#0000FF";
    } elseif($_SESSION['user']['account_type'] == 4) {
        $account_type = "Administrator";
        $color = "#DF0101";
    }
    if(isset($_POST['sticky'])){
        $sticky = 1;
    } else {
        $sticky = 0;
    }






if(!isset($error)){



/* INSERT INTO TOPICS TABLE */
    $query = "INSERT INTO bkg_topics (
            forum_id,
            icon_id,
            topic_approved,
            topic_title,
            topic_text,
            topic_poster_id,
            topic_poster,
            topic_poster_color,
            topic_post_time,
            topic_status,
            topic_type
        ) VALUES (
            :forumid,
            :iconid,
            :topicapproved,
            :topictitle,
            :topictext,
            :topicposter_id,
            :topicposter,
            :topicposter_color,
            :topicpost_time,
            :topicstatus,
            :topictype

        )";
    $query_params = array(
        ':forumid' => $fid,
        ':iconid' => 1,
        ':topicapproved' => 1,
        ':topictitle' => $_POST['title'],
        ':topictext' => $_POST['content'],
        ':topicposter_id' => $_SESSION['user']['id'],
        ':topicposter' => $_SESSION['user']['displayname'],
        ':topicposter_color' => $color,
        ':topicpost_time' => time(),
        ':topicstatus' => 0,
        ':topictype' => $sticky
    );

    $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);

        $lastid = $db->lastInsertId();
         /* Retrieve the last id of a topic, used to generate some links. */
        /* UPDATE FORUM TABLE */

    $query = "UPDATE bkg_forums SET
        `forum_last_post_id` = :lastpostid,
        `forum_last_post_topic_id` = :lastposttopicid,
        `forum_last_post_title` = :lastposttitle,
        `forum_last_poster_id` = :lastposterid,
        `forum_last_post_time` = :lastposttime,
        `forum_last_poster_name` = :lastpostername,
        `forum_last_poster_color` = :lastpostercolor
    WHERE `forum_id` = :forumid
    ";
    $query_params = array(
        ':lastpostid' => null,
        ':lastposttopicid' => $lastid,
        ':lastposttitle' => $_POST['title'],
        ':lastposterid' => $_SESSION['user']['id'],
        ':lastposttime' => time(),
        ':lastpostername' => $_SESSION['user']['displayname'],
        ':lastpostercolor' => $color,
        ':forumid' => $fid
    );

    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);

    if($fid == 13){
        $query = "INSERT INTO updates (
            title,
            content,
            `date`,
            `user`,
            `topic_id`
        ) VALUES (
            :title,
            :content,
            :date_posted,
            :user_posted,
            :topic_id
        )";
        $query_params = array(
            ':title' => $_POST['title'],
            ':content' => $_POST['content'],
            ':date_posted' => time(),
            ':user_posted' => $_SESSION['user']['displayname'],
            ':topic_id' => $lastid
        );
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }


    try {
        $db->commit();
        $post_ok = 1;
    } catch(PDOException $e) {
        $erroradmin[] = $e->getMessage();
        $db->rollback();
    }
    if(isset($post_ok)): ?>
        <script>
            location.href = "http://www.boundlessknights.com?viewtopic&fid=<?php echo $fid; ?>&tid=<?php echo $lastid; ?>";
        </script>
    <?php else: ?>
        <?php $error[] = "Your topic did not post."; ?>
    <?php endif; ?>
    <?php 
}
}
?>

Questions I looked at:

Form Post Not Reading Any Value

Cannot Get the Value of a Textarea via Post Method

Textarea Not Posting with Form

Textarea Returns Empty Value in PHP Post

Community
  • 1
  • 1
Moonblaze
  • 163
  • 1
  • 11
  • 1
    Simple way to test sended data is writing `var_dump($_POST);` at beginning of php file – Veniamin Mar 25 '16 at 08:44
  • can you please explain it more widely ? so i can help you out – KAREN Mar 25 '16 at 08:44
  • Currently at this point the form is NOT being submitted, because of the html required attribute in the textarea tag. I initially had a check to see if either title or content were empty, with `if(empty($_POST['content'])){ error msg }`, and it would display my error message I had displayed. However, when I was doing the check with the latter proccess, the `$_POST` array didn't have anything in content, meaning it's empty. – Moonblaze Mar 25 '16 at 08:47
  • @Moonblaze is there any error ? – KAREN Mar 25 '16 at 08:49
  • @Talhiner As I said with my intial post, with the html attribute on the tag, it won't allow me to post it, and tells me to fill out that particular field. When I was using the PHP Check, my error message told me that the field was empty.. For whatever reason the form is not reading the input that I type into the textarea. – Moonblaze Mar 25 '16 at 08:50
  • @Moonblaze Does it showing a null value ? – KAREN Mar 25 '16 at 08:55
  • Yes. When I go to post the form, it is null.... – Moonblaze Mar 25 '16 at 09:01
  • @Moonblaze Check my another Answer please – KAREN Mar 25 '16 at 09:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107312/discussion-between-talhiner-and-moonblaze). – KAREN Mar 25 '16 at 09:16
  • You're not showing enough HTML/JS code. You probably have some other required field on the same page or you are filling out some other textarea. – nl-x Mar 25 '16 at 09:17
  • You are using TinyMCE editor. Worth mentioning. I'll add it to your tags. Looking at your site, I see the following appearing: `An invalid form control with name='content' is not focusable.` – nl-x Mar 25 '16 at 09:31

5 Answers5

2

TinyMCE does not keep the underlying textarea in sync at all times. Normally, when you post the form, TinyMCE will update the textarea before the form is posted but the process seems to be stopped by the required attribute. You can use the following API call to force TinyMCE to update the textarea:

tinymce.triggerSave();

This will force TinyMCE to update the textarea when its called. You can either:

  • Do this in the onsubmit event of the form
  • Do this in the TinyMCE init:

    tinymce.init({
        selector: "textarea",
        setup: function (editor) {
            editor.on('change', function () {
                tinymce.triggerSave();
            });
        }
    });
    
Michael Fromin
  • 13,131
  • 2
  • 20
  • 31
1

Your page is using TinyMCE editor. It is giving the following error in the console: An invalid form control with name='content' is not focusable. Fixing that will fix your problem.

nl-x
  • 11,762
  • 7
  • 33
  • 61
0

Hmmm, did you try to remove this "form" attribute from your Textarea ?

<textarea name="content" id="content" required></textarea>

Tell us what it do when u try.

Lior CHAMLA
  • 128
  • 8
  • Nothing has changed; With the required attribute on the textarea tag, still tells me I need to fill out the field. – Moonblaze Mar 25 '16 at 08:58
  • Yup, this is logical. The required attribute specify that you HAVE to enter data on the textarea .. If you want to allow an empty content for your comment, juste remove this "required" attribute. I was talking about your "form" attribute in your textarea. – Lior CHAMLA Mar 25 '16 at 09:00
  • Look at the answer that just gave Talhiner. He wrote what I wanted you to do. Test it and tell us ;-) – Lior CHAMLA Mar 25 '16 at 09:02
  • Like my first commend, I tried what you suggested. It didn't work. My error that I am receiving has not changed. – Moonblaze Mar 25 '16 at 09:04
0

You might not be able to post anything because you've NOT specified the action attribute of your form.

<form action="" method="post" id="CreateTopicForm">

Set it to the name of the php file (with the proper path to the file), and it should work.

Note: To make sure the the $_POST array contains your form submitted values, do a var_dump($_POST).

Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
  • the PHP code is currently on the same page, I have never specified the action, when the code is on the same page... When I was doing the check if the field was empty with PHP, it was posting, but the form was not reading the contents in the textarea. – Moonblaze Mar 25 '16 at 08:57
  • My issue is not with the form not posting. It's the form is not reading the textarea input. – Moonblaze Mar 25 '16 at 09:00
  • If you're not capturing the posted values from the form, how do you know that the form is not reading the textarea input? Do you mean that _even when the textarea is filled_ you get HTML5 validation message? – Ahmad Baktash Hayeri Mar 25 '16 at 09:10
  • Yes, Even when the textarea is filled, I get the HTML5 Validation message. – Moonblaze Mar 25 '16 at 09:11
0

Change this

 <textarea name="content" id="content" required="required" form="CreateTopicForm"></textarea>

to this

  <textarea name="content" id="content" required="required" ></textarea>
KAREN
  • 388
  • 2
  • 10