[英]html form not displaying values
Scenario: I have this simple HTML 5 Form inside the PHP tag.场景:我在 PHP 标签中有这个简单的 HTML 5 表格。
<?php
echo '
<form method="post" action="index.php">
<input type="email" name="email value="<?php echo $email; ?>">
<input type="password" name="password">
<button type="submit" name="signin">Sign in</button>
</form>
';
?>
The form displays correctly but I can not get the value of $email, it just displays as raw text.表单显示正确,但我无法获取 $email 的值,它只显示为原始文本。
So, how to echo a php variable from a form inside a PHP tag.那么,如何从 PHP 标记内的表单中回显 php 变量。
FINAL CODE最终代码
<?php
echo '<form method="post" action="index.php" class="navbar-form navbar-right">
<input type="email" name="email" placeholder="Email" class="form-control1" value="' . $email . '">
<input type="password" name="password" placeholder="Password" class="form-control">
<button type="submit" name="signin">Sign in</button>
</form>';
?>
Try Closing the Form tag:尝试关闭表单标签:
<?php
echo '
<form method="post" action="index.php">
<input type="email" name="email"value="<?php echo $email; ?>">
<input type="password" name="password">
<button type="submit" name="signin">Sign in</button>
</form>
';
?>
You need to escape out from the string with '
and concatenate with .
您需要使用
'
从字符串中转义并与 . 连接.
that way you can use php variables into an string这样您就可以将 php 变量转换为字符串
<?php
echo '
<form method="post" action="index.php">
<input type="email" name="email "value="' . $email .'">
<input type="password" name="password">
<button type="submit" name="signin">Sign in</button>
</form>
';
?>
Or like said in the comment, place the html code after your php closing tag或者像评论中所说的那样,将 html 代码放在 php 结束标签之后
<?php
// Do some code
?>
<form method="post" action="index.php">
<input type="email" name="email "value="<?= $email; ?>">
<input type="password" name="password">
<button type="submit" name="signin">Sign in</button>
</form>
if you want to access your variables for an echo statement you have to use double quotes.如果要访问 echo 语句的变量,则必须使用双引号。 and you can put single quotes inside double quotes.
您可以将单引号放在双引号内。
$email = "example@gmail.com";
echo "
<form method='post' action='index.php'>
<input type='email' name='email' value='$email'>
</form>
";
or even using Heredoc甚至使用Heredoc
$email = "example@gmail.com";
echo <<<HTML
<form method="post" action="index.php">
<input type="email" name="email" value="$email">
<input type="password" name="password">
<button type="submit" name="signin">Sign in</button>
</form>
HTML;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.