[英]html form not displaying values
场景:我在 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>
';
?>
表单显示正确,但我无法获取 $email 的值,它只显示为原始文本。
那么,如何从 PHP 标记内的表单中回显 php 变量。
最终代码
<?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>';
?>
尝试关闭表单标签:
<?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>
';
?>
您需要使用'
从字符串中转义并与 . 连接.
这样您就可以将 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>
';
?>
或者像评论中所说的那样,将 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>
如果要访问 echo 语句的变量,则必须使用双引号。 您可以将单引号放在双引号内。
$email = "example@gmail.com";
echo "
<form method='post' action='index.php'>
<input type='email' name='email' value='$email'>
</form>
";
甚至使用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.