在文本输入 value = 语句中使用 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4446727/
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 13:04:40  来源:igfitidea点击:

Using a PHP variable in a text input value = statement

phphtmlvariablesinput

提问by malcolm laplante

I retrieve three pieces of information from the database, one integer, one string, and one date.

我从数据库中检索了三条信息,一个整数、一个字符串和一个日期。

I echo them out to verify the variables contain the data.

我回显它们以验证变量包含数据。

When I then use the variables to populate three input boxes on the page, they do not populate correctly.

当我然后使用变量填充页面上的三个输入框时,它们没有正确填充。

The following do not work:

以下不起作用:

id: <input type="text" name="idtest" value=$idtest>

Yes, the variable must be inside <?php var ?> for it to be visible.

是的,变量必须在 <?php var ?> 内才能可见。

So:

所以:

id: <input type="text" name="idtest" value=<?php $idtest ?> />

The field displays /.

该字段显示/

When I escape the quotes,

当我逃避引号时,

id: <input type="text" name="idtest" value=\"<?php $idtest ?>\"  />

the field then displays \"\".

然后该字段显示\"\"

With single quotes

带单引号

id: <input type="text" name="idtest" value='<?php $idtest ?>'  />

the field displays nothing or blank.

该字段不显示任何内容或空白。

With single quotes escaped,

单引号转义后,

id: <input type="text" name="idtest" value=\'<?php $name ?>\'  />

the field displays \'\'.

字段显示\'\'

With a forward slash (I know that's not correct, but to eliminate it from the discussion),

使用正斜杠(我知道这是不正确的,但要从讨论中删除它),

id: <input type="text" name="idtest" value=/"<?php $name ?>/"  />

the field displays /"/".

字段显示/"/"

Double quotes, escape double quotes, escape double quotes on left side only, etc. do not work.

双引号、转义双引号、仅在左侧转义双引号等都不起作用。

I can set an input box to a string. I have not tried using a session variable as I prefer to avoid do that.

我可以将输入框设置为字符串。我没有尝试使用会话变量,因为我更愿意避免这样做。

What am I missing here?

我在这里缺少什么?

回答by icktoofay

Try something like this:

尝试这样的事情:

<input type="text" name="idtest" value="<?php echo htmlspecialchars($name); ?>" />

That is, the same as what thirtydot suggested, except preventing XSS attacks as well.

也就是说,与三十点建议的相同,除了还要防止 XSS 攻击。

You could also use the <?=syntax(see the note), although that might not work on all servers. (It's enabled by a configuration option.)

您也可以使用<?=语法(请参阅注释),尽管这可能不适用于所有服务器。(它由配置选项启用。)

回答by thirtydot

You need, for example:

你需要,例如:

<input type="text" name="idtest" value="<?php echo $idtest; ?>" />

The echofunction is what actually outputs the value of the variable.

echo功能是实际输出变量的值。

回答by 8084635

From the HTML point of view everything's been said, but to correct the PHP-side approach a little and taking thirtydot's and icktoofay's advice into account:

从 HTML 的角度来看,一切都已经说完了,但要稍微纠正 PHP 端的方法并考虑到三十多和 icktoofay 的建议:

<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($idtest) . '">'; ?>

回答by jckhan

Solution

解决方案

You are missing an echo. Each time that you want to show the value of a variable to HTML you need to echo it.

你错过了echo。每次您想要向 HTML 显示变量的值时,您都需要回显它。

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

Note: Depending on the value, your echo is the function you use to escape it like htmlspecialchars.

注意:根据值,您的 echo 是您用来像 htmlspecialchars 一样转义它的函数。

回答by John Don

If you want to read any created function, this how we do it:

如果你想阅读任何创建的函数,我们这样做:

<input type="button" value="sports" onClick="window.open('<?php sports();?>', '_self');">

回答by PHP LORD

I have been doing PHP for my project, and I can say that the following code works for me. You should try it.

我一直在为我的项目做 PHP,我可以说以下代码对我有用。你应该试试看。

echo '<input type = "text" value = '.$idtest.'>';