How to retain textarea contents when PHP page is refreshed?

末鹿安然 提交于 2019-12-08 13:00:00

问题


My dilemma is this: I have a PHP page with a form that includes a textarea where the user can enter any kind of text. The code below is at the top part of my PHP page, to check whether or not the user has pressed the "Submit" button for the form, and to error-check all the user input:

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

The code below is the HTML/PHP code for the form, and the textarea specifically:

...
// 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 />';
...

When I enter, let's say:

Testing testing...

...

<>// HELLO!!!

Into the textarea, but then fail one of the other checks on the page so the form/page is refreshed and shows an error, I want to retain what the user wrote in the textarea. But with the code I have, the stored text that is displayed turns into:

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

How can I "save" the textarea contents so it is identical to what the user wrote before the PHP page is refreshed? Can't think of a solution. :( Many thanks in advance!


回答1:


You should echo the original $_POST['location'] value (with htmlentities), not the trimmed and mysql_real_escaped version of it.

$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 />';



回答2:


just skip impose on $loc = mysqli_real_escape_string($dbc, trim($_POST['location']));

htmlentities($loc) or htmlentities($_POST['location']) should be good enough to return what user just submitted




回答3:


If the \r\n are the only problem you could perform a str_replace() before you send it back to the browser:

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

If there are other problems as well, this method might not be the most ideal one.




回答4:


You could try to replace

htmlentities($loc)

with

htmlentities(stripslashes($loc))

since it's probably mysqli_real_escape() who breaks \r\n.

Or you could try to output the raw POST data

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



回答5:


if your form method is post, you can simply give your user raw post data . $_POST['location'] . and use your "cleared" input just to insert it to db




回答6:


Use Cookie. Here's googling sample code. Here's 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:


Add wrap to your textarea

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

becomes:

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

You can also use wrap: off, hard, soft, physical

Your textinput should now be wrapped exactly as you wrote it.



来源:https://stackoverflow.com/questions/4202825/how-to-retain-textarea-contents-when-php-page-is-refreshed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!