Textarea没有阅读任何输入

时间:2016-03-25 08:36:16

标签: php html forms tinymce

textarea不会读取输入框中输入的任何内容。最初,我使用PHP检查textarea是否为空,并在那里收到错误。所以我删除了那个检查,看看它是否是导致问题的php,并将required="required"属性添加到textarea标签,甚至那回来了Please fill out this field.我不太确定我的代码出错了,我以前工作过,然后突然间它停止工作了,我完全不知道为什么。我还看了关于textarea没有提交的其他各种帖子,并确保我用姓名而不是ID来检查帖子;并确保textarea提交与提交按钮相同的表单。我也试过了,没有在textarea标签上指定表格。

HTML代码:

    <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代码:

/* 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 
}
}
?>

我看过的问题:

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

5 个答案:

答案 0 :(得分:1)

您的网页正在使用TinyMCE编辑器。它在控制台中出现以下错误:An invalid form control with name='content' is not focusable. 修复将解决您的问题。

答案 1 :(得分:1)

TinyMCE不会始终保持基础textarea同步。通常,当您发布表单时,TinyMCE将在发布表单之前更新textarea,但该过程似乎已被所需属性停止。您可以使用以下API调用强制TinyMCE更新textarea:

tinymce.triggerSave();

这将迫使TinyMCE在被调用时更新textarea。你可以:

  • 在表格
  • 的onsubmit事件中执行此操作
  • 在TinyMCE init中执行此操作:

    tinymce.init({
        selector: "textarea",
        setup: function (editor) {
            editor.on('change', function () {
                tinymce.triggerSave();
            });
        }
    });
    

答案 2 :(得分:0)

嗯,您是否尝试从Textarea中删除此“表单”属性?

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

告诉我们你在尝试时做了什么。

答案 3 :(得分:0)

您可能无法发布任何内容,因为您指定了表单的action属性。

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

将其设置为php文件的名称(具有文件的正确路径), 它应该工作。

注意:要确保$_POST数组包含表单提交的值,请执行var_dump($_POST)

答案 4 :(得分:0)

更改此

try
{   
    string VNumber = null;      //outside the loop
    while (DR.Read())
    {
        #region Fetching DB data
        DateTime TimeStamp = (DateTime)DR["ExceptionDate"];
        VNumber = (string)DR["VisitNumber"];
        Console.Write("Total Visits = " +VNumber "\n");
        #endregion
        DR.Close();
    }

    cmd = new SqlCommand("UPDATE People SET Visits = '0' WHERE VisitNumber = '" + VNumber + "'", connection);   //The name 'VNumber' does not exist in the current context
    cmd.ExecuteNonQuery();
}

到这个

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