如何保留PHP页面刷新时的textarea内容? [英] How to retain textarea contents when PHP page is refreshed?

查看:101
本文介绍了如何保留PHP页面刷新时的textarea内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的困境是这样的:我有一个PHP页面,其中包含一个 textarea 的表单,用户可以在其中输入任何类型的文本。下面的代码位于我的PHP页面的顶部,用于检查用户是否按下了表单的Submit按钮,并检查所有的用户输入:

 <?php 
//检查用户是否按前面的提交按钮
if(isset($ _ POST ['submit']) ){
...
$ loc = mysqli_real_escape_string($ dbc,trim($ _ POST ['location']));
...

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

  ... 
// location texarea entry
echo'< label for =location>会议地点:< / label>< br />';
echo'< textarea name =locationid =locationcols =40rows =5>'。 htmlentities($ loc)。 '< / textarea>< br />';
$ b $ // submit button
echo'< input type =submitvalue =Submitname =submit/>< br />>';
...

当我进入时,让我们说一下:

 测试测试... 

...

<> // HELLO !! !

进入textarea,但是失败了页面上的其他检查之一,所以form / page是刷新并显示错误,我想保留用户在textarea中写的内容。但是通过我所拥有的代码,所显示的存储文本变成:

 测试测试... \r\ n \r\\\
... \r\\\
\r\\\
<> // HELLO !!!

如何保存textarea内容,以便与用户在PHP之前编写的内容相同页面刷新了吗?想不到一个解决方案。 :(非常感谢!

解决方案

您应该回显原始 $ _ POST ['location'] value(带有 htmlentities ),而不是它的trimmed和mysql_real_escaped版本。

<$ p $(p * POST * /){
$ loc = $ _POST ['location'];
... $
$ query ='INSERT INTO ...'。mysql_real_escape_string(trim($ loc))。''';
...
}

echo' < textarea name =locationid =locationcols =40rows =5>'.htmlentities($ loc)。'< / textarea>< br />';


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!

解决方案

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

这篇关于如何保留PHP页面刷新时的textarea内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆