开发者

How to retain textarea contents when PHP page is refreshed?

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:

...
/开发者_StackOverflow/ 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!


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


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


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.


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


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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜