[英]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.