使 PHP 变量成为表单输入的值

Make a PHP variable the value of a form Input

我想使 PHP 变量成为隐藏形式 input 的值。该表格在我的 PHP 中(我正在回应表格),但我尝试过的任何方法都不起作用。

这是我的代码:

echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<!-- Here is where I need to make my PHP variable the value: -->
<input type = "text" name = "referer" style = "display: none" value = "$variable"> 

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';

通常的字符串替换 "$var" 在这里不起作用,因为它全部包含在不允许字符串替换的单引号字符串中。您将必须手动连接

echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<input type = "text" name = "referer" style = "display: none" value = "' . $variable . '"> 

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';

试试这个:

?><!-- exit out of php into html-->
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<!--Here is where I need to make my PHP variable the value:-->
<input type = "text" name = "referer" style = "display: none" value = "<?=$variable?>">

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
<?php // enter back into php

<?= ?>是一个php short tag


此外,如果您仍想使用 echo,请尝试以下操作:

//note: I changed the quotes
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>

<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'> 

<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";

有关详细信息,请参阅 this Q/A

在此处尝试此示例 https://eval.in/506642

   echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>

//Here's where I need to make my PHP variable the value:
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'> 

<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";