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

时间:2022-08-22 08:53:20

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.

是的,变量必须在 使其可见。

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?

我错过了什么?

6 个解决方案

#1


33  

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.

这和thirtydot的建议一样,除了防止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.)

您还可以使用

#2


6  

You need, for example:

你需要,例如:

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

The echo function is what actually outputs the value of the variable.

echo函数实际上输出变量的值。

#3


2  

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-side的方法,并将thirtydot和icktoofay的建议考虑进去:

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

#4


2  

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.

你错过了一个回音。每次要向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那样逃离它的函数。

#5


0  

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

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

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

#6


-2  

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

#1


33  

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.

这和thirtydot的建议一样,除了防止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.)

您还可以使用

#2


6  

You need, for example:

你需要,例如:

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

The echo function is what actually outputs the value of the variable.

echo函数实际上输出变量的值。

#3


2  

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-side的方法,并将thirtydot和icktoofay的建议考虑进去:

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

#4


2  

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.

你错过了一个回音。每次要向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那样逃离它的函数。

#5


0  

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

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

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

#6


-2  

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