使PHP变量表单输入的值

时间:2016-01-23 03:35:36

标签: html forms php-5.4

我需要将PHP变量设为隐藏表单输入的值。表单在我的PHP内部(我回复表单),我尝试的任何东西都没有。这是我的代码:

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

3 个答案:

答案 0 :(得分:0)

通常的字符串替换"$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>
';

答案 1 :(得分:0)

试试这个:

?><!-- 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

答案 2 :(得分:0)

在此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>
";