提交表格后,Textarea不会保留其内容

时间:2018-01-25 20:09:47

标签: php html forms post textarea

我正在尝试在提交表单后在其字段中保留textarea内容。

每次单击提交按钮时,我都会丢失textarea的内容,因为textarea已被清除。

表单和textarea代码:

<form method="post" action="ProcessText.php" role="form" id="TextEntry">
                    <fieldset style=" height: auto; margin-left: 4%; margin-top: 4%; margin-right: 4%; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
                        <legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Text Entry</b></legend>
                        <div class="form-group" style=" margin-bottom: 3px;margin-top:20px; margin-left: 5%; margin-right: 5%;">
                            <div style="position: absolute; margin-top: 5px"><label for="TextArea" class="label label-primary" >Text Area</label></div><br>
                            <div style="position: relative"><textarea style=" resize: none" id = "TextArea" name="TextArea" class="form-control"
                                                                      rows = "19"
                                                                      cols = "50"
                                                                      style=" width: auto; height: auto; text-align: left; border-right: 1px solid#c0c0c0; " placeholder="Add some text"><?php if(isset($_POST['TextArea'])) { echo htmlentities ($_POST['TextArea']); }?></textarea></div>        
                        </div>
                        <br><button style="float: right; margin-right: 5%" type="submit" class="btn btn-success" name="Submit">Analyse</button>
                    </fieldset>
                </form>

表单和textarea位于index.php页面中,该页面提交到另一个页面(即ProcessText.php)。提交成功运行,ProcessText.php页面按预期接收发布的数据。

我已经浏览了论坛以寻找答案并尝试了许多解决方案,例如将以下代码放在textarea标签中:

<?php if(isset($_POST['textarea1'])) { 
         echo htmlentities ($_POST['textarea1']); }?>

我也在textarea标签中尝试了这个代码而没有任何好处:

<?php echo $_POST['TextArea'] ? $_POST['TextArea'] : "" ?>

只是为了澄清,包含textarea的index.php页面代码:

<html>
    <head>
        <title>Text Sorting Form</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="css/Styles.css"/>
        <script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
        <script src="js/bootstrap.min.js" type="text/javascript"></script>
        <style> </style>
    </head>
    <body>


        <div style="width: 100%; overflow: hidden;">

            <div style=" background-color: white; border: 1px solid Grey; 
                 border-radius: 3px; box-shadow: 3px 0px 3px #888888; 
                 width:50%; max-width: 50%; height: 90%; margin-top:3%; margin-left: 
                 3%; margin-right:auto; overflow: hidden; float: left;">
                <form method="post" action="ProcessText.php" role="form" id="TextEntry">
                    <fieldset style=" height: auto; margin-left: 4%; margin-top: 4%; margin-right: 4%; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
                        <legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Text Entry</b></legend>
                        <div class="form-group" style=" margin-bottom: 3px;margin-top:20px; margin-left: 5%; margin-right: 5%;">
                            <div style="position: absolute; margin-top: 5px"><label for="TextArea" class="label label-primary" >Text Area</label></div><br>
                            <div style="position: relative"><textarea style=" resize: none" id = "TextArea" name="TextArea" class="form-control"
                                                                      rows = "19"
                                                                      cols = "50"
                                                                      style=" width: auto; height: auto; text-align: left; border-right: 1px solid#c0c0c0; " placeholder="Add some text"></textarea></div>        
                        </div>
                        <br><button style="float: right; margin-right: 5%" type="submit" class="btn btn-success" name="Submit">Analyse</button>
                    </fieldset>
                </form>
            </div>


            <div style=" background-color: white; border: 1px solid Grey; 
                 border-radius: 3px; box-shadow: 3px 0px 3px #888888; 
                 width:40%; margin-left: 700px; height: 90%; margin-top:3%;">
                <fieldset style=" height:90%; margin-left: 4%; margin-top: 5%; margin-right: 4%; margin-bottom: 10px; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
                    <legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Result</b></legend>
                    <div class="Notes" style=" margin-bottom: 3px;margin-top:40px; margin-left: 5%; margin-right: 5%; height: 75%; width: auto">

                        <?php
                        session_start();

                        // Display Custom Field Value
                        echo '<pre>';
                        echo nl2br("The Submitted Paragraph:\n".$_SESSION['Text']."\n\n");
                        echo nl2br ("Number of Words: " . $_SESSION['number']."\n\n");
                        echo nl2br("Word Frequency in Descending Order:\n");
                        print_r($_SESSION['results']);
                        echo '</pre>';
                        session_unset();
                        session_destroy();
                        ?>

                    </div>
                </fieldset>

            </div>

        </div>     
    </body>
</html>

ProcessText.php页面代码:

<html>
    <head>
        <title>Text Sorting Form</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="css/Styles.css"/>
        <script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
        <script src="js/bootstrap.min.js" type="text/javascript"></script>
        <style>

        </style>
    </head>
    <body>
        <?php session_start(); ?>

        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {


            $text = $_POST['TextArea'];
            $wordcount = word_count ($text);
            $words = utf8_str_word_count($text, 1);
            $frequency = array_count_values($words);
            arsort($frequency);
            $_SESSION['results'] = $frequency;
            $_SESSION['number'] = $wordcount;
            $_SESSION['Text'] = $text;
            header("location: index.php");
            exit();

        };

        function utf8_str_word_count($string, $format = 0, $charlist = null) {
            $result = array();


            if (preg_match_all('~[\p{L}\p{Mn}\p{Pd}\'\x{2019}' . preg_quote($charlist, '~') . ']+~u', $string, $result) > 0) {
                if (array_key_exists(0, $result) === true) {
                    $result = $result[0];
                }
            }

            if ($format == 0) {
                $result = count($result);
            }

            return $result;
        }

        function word_count ($string) {
            $words_to_count = strip_tags($string);
            $pattern = "/[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/";
            $words_to_count = preg_replace ($pattern, " ", $words_to_count);
            $words_to_count = trim($words_to_count);
            $total_words = count(explode(" ",$words_to_count)); 
            return $total_words;
        }

        ?> 
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

您面临的主要问题是您正在发布到其他脚本,然后将header("location: index.php");发送回index.php。这会丢失所有$ _POST变量,并且无法将它们添加到标题重定向中(浏览器POST,而不是服务器标头重定向)。

所以,你需要改变方向......

由于您已经将textarea内容填充到会话变量$_SESSION['Text']中,因此您可以在index.php上使用它:

<textarea name="TextArea" ...otherstuff... ><?php 
      echo (!empty($_SESSION['Text']) ? htmlspecialchars($_SESSION['Text']) : '' );
      unset($_SESSION['Text']);// optionally clear after re-iterating it
?></textarea>       

您需要在ProcessText.php页面中解决一些注意事项:

  • 在任何html或echo输出之前将session_start();移动到最顶层。
  • session_write_close();
  • 之前添加header();exit;
  • 哎呀,你header()之上的所有'html'都需要也好。

如果你不想使用会话......因为有些专家告诉你他们非常不可靠和无用:

A)发布到 同一页面 ,这将重新显示您的表单(即index.php)

B)设置一个主分支脚本来处理所有提交,但没有自己的html。然后,这将直接include('index.php');您应该给出哪个页面(因此$ _POST变量将存在)。但是,这种方法需要在设置所有脚本和html块时进行一些规划和规范。

答案 1 :(得分:0)

在捕获错误集$_SESSION['textentered']的那一点。例如,如果您不想在数据库中重复值。 示例:

//condition that makes you echo back entered values when there is an error
if($rowexists){
$_SESSION['textentered']=$_POST['textentered'];
}

然后在您的文本区域中执行以下操作:

<textarea><?php if(isset($_SESSION['textentered']) && $_SESSION['textentered'] !=''){echo $_SESSION['textentered'];} ?></textarea>

这对我有用。