如何通过 PHP 设置文本框的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1484816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:46:28  来源:igfitidea点击:

How can I set the value of a textbox through PHP?

php

提问by Carlo

So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.

所以我在注册页面中有这个空的文本框。用户输入一些数据,点击继续,然后出现一个确认页面。如果数据不正确,用户点击返回以纠正错误。然而,当他回去时,所有的文本框都是空的。所以我想到的第一件事就是将用户数据存储在一个会话中(我有一个保存所有这些数据的用户类,所以我将这个类存储在会话中)。当用户返回时,我能够检索数据。

I do something like this:

我做这样的事情:

if($_SESSION['UserInfo'])
{
    $user = $_SESSION['UserInfo'];

    $firstName = $user->FirstName;
    $lastName = $user->LastName;
}

How would I put these variables in a textbox?

我如何将这些变量放在文本框中?

回答by brianreavis

To set the value, you can just echo out the content inside the valueattribute:

要设置值,您可以只回显value属性内的内容:

<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />

回答by SeanJA

Of course you will want to escape it but...

当然你会想逃避它,但是......

<input type="text" value="<?php echo $firstName ?>" />

or if the form is posted, it would be easier to do:

或者如果表格被张贴,这样做会更容易:

<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />

fine... even though it was out of the scope of the question here is the escaped version:

很好......即使它超出了问题的范围,这里是转义版本:

<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />

回答by unknown

smth like

喜欢

<input type="text" value="<?php echo $first_name;?>">

Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.

不要忘记使用 htmlentities() 或类似的方法进行转义。如果你不知道为什么 - 谷歌 XSS。