【问题标题】:How to retain textarea contents when PHP page is refreshed?PHP页面刷新时如何保留textarea内容?
【发布时间】:2010-11-17 08:54:30
【问题描述】:

我的困境是:我有一个 PHP 页面,其表单包含一个 textarea,用户可以在其中输入任何类型的文本。下面的代码在我的 PHP 页面的顶部,用于检查用户是否按下了表单的“提交”按钮,并对所有用户输入进行错误检查:

<?php 
// check if user has pressed the Submit button earlier
if (isset($_POST['submit'])) {
    ...
    $loc = mysqli_real_escape_string($dbc, trim($_POST['location']));
    ...

下面的代码是表单的 HTML/PHP 代码,特别是 textarea:

...
// location texarea entry
echo '<label for="location">Meeting location: </label><br />';
echo '<textarea name="location" id="location" cols="40" rows="5">' . htmlentities($loc) . '</textarea><br />';

// submit button
echo '<input type="submit" value="Submit" name="submit"/><br />';
...

当我进入时,让我们说:

Testing testing...

...

<>// HELLO!!!

进入 textarea,但随后未能通过页面上的其他检查之一,因此刷新表单/页面并显示错误,我想保留用户在 textarea 中写的内容。但是使用我拥有的代码,显示的存储文本变成:

Testing testing...\r\n\r\n...\r\n\r\n<>// HELLO!!!

如何“保存” textarea 内容,使其与刷新 PHP 页面之前用户所写的内容相同?想不出解决办法。 :( 提前非常感谢!

【问题讨论】:

    标签: php html textarea html-entities


    【解决方案1】:

    您应该回显原始的 $_POST['location'] 值(带有 htmlentities),而不是它的修剪和 mysql_real_escaped 版本。

    $loc = null;
    if (/* POST */) {
        $loc = $_POST['location'];
        ...
        $query = 'INSERT INTO ... "' . mysql_real_escape_string(trim($loc)) . '"';
        ...
    }
    
    echo '<textarea name="location" id="location" cols="40" rows="5">' . htmlentities($loc) . '</textarea><br />';
    

    【讨论】:

    • 天哪,为什么我没有想到只使用 $_POST 值...现在我觉得很愚蠢。非常感谢您的出色回答,也感谢其他所有人!
    【解决方案2】:

    直接跳过$loc = mysqli_real_escape_string($dbc, trim($_POST['location']));

    htmlentities($loc)htmlentities($_POST['location']) 应该足以返回用户刚刚提交的内容

    【讨论】:

      【解决方案3】:

      如果\r\n 是唯一的问题,您可以在将str_replace() 发送回浏览器之前执行它:

      $loc = str_replace("\\r\\n", "\n", $loc);
      

      如果还有其他问题,这种方法可能不是最理想的。

      【讨论】:

        【解决方案4】:

        你可以尝试替换

        htmlentities($loc)
        

        htmlentities(stripslashes($loc))
        

        因为破坏\r\n的可能是mysqli_real_escape()。

        或者你可以尝试输出原始的 POST 数据

        echo '<textarea name="location" id="location" cols="40" rows="5">' . $_POST['location'] . '</textarea><br />';
        

        【讨论】:

          【解决方案5】:

          如果你的表单方法是 post,你可以简单地给你的用户原始 post 数据. $_POST['location'] . 并使用你的“清除”输入只是将它插入到 db

          【讨论】:

            【解决方案6】:

            使用 Cookie。 这是谷歌搜索示例代码。 这是html >>

            http://skymong9.egloos.com/1797665

            <HTML>
            <HEAD>
            <TITLE>BLUE-B</TITLE>
            <script>
            function ReadCookie (Name)
            {
             var search = Name + "="
             if (document.cookie.length > 0)
             {
              offset = document.cookie.indexOf(search)
              if (offset != -1)
              {
               offset += search.length
               end = document.cookie.indexOf(";", offset)
               if (end == -1)
                end = document.cookie.length
               return (document.cookie.substring(offset, end))
              }
              else
               return ("");
             }
             else
              return ("");
            }
            
            function WriteCookie (cookieName, cookieValue, expiry)
            {
             var expDate = new Date();
            
             expDate.setTime (expDate.getTime() + expiry);
             document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString() + "; path=/";
            }
            
            function getCookies()
            {
             document.noteForm.note.value = unescape(ReadCookie("note"));
            }
            </script>
            
            </head>
            <body>
            <form NAME="noteForm">
            <textarea ROWS="5" COLS="40" NAME="note" WRAP="VIRTUAL"></textarea>
            
            <input TYPE="button" VALUE="Save Text" onClick="WriteCookie('note', document.noteForm.note.value, 2678400000)">
            <INPUT TYPE="button" VALUE="새로고침" onClick='parent.location="javascript:location.reload()"'>
            </form>
            <script>
            window.onload=function() { getCookies(); }
            </script>
            

            【讨论】:

              【解决方案7】:

              wrap 添加到您的文本区域

              <textarea name="location" id="location" cols="40" rows="5">
              

              变成:

              <textarea name="location" id="location" cols="40" rows="5" wrap="virtual">
              

              您还可以使用 wrap: off、hard、soft、physical

              您的 textinput 现在应该完全按照您编写的方式包装。

              【讨论】:

                猜你喜欢
                • 2023-03-28
                • 2015-06-12
                • 1970-01-01
                • 1970-01-01
                • 2014-07-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多