【问题标题】:html form not displaying valueshtml表单不显示值
【发布时间】:2021-06-29 12:32:59
【问题描述】:

场景: 我在 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>'; 
?>

【问题讨论】:

  • 在 name 和 value 属性之间放一个空格,你会回显到回显中。我认为这行不通,只需将 html 字符串放在 php 结束标记之后

标签: php forms


【解决方案1】:

如果你想为 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;

【讨论】:

    【解决方案2】:

    您需要使用' 退出字符串并与. 连接,这样您就可以将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>
    

    【讨论】:

      【解决方案3】:

      尝试关闭表单标签:

      <?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 echo $email 识别为值
      • 我有一段时间没有使用 PHP,只是注意到缺少一个表单标签,所以我想我至少会提供帮助。我似乎记得使用 $_POST 数组和数据进入那里。不确定这是否有帮助,但可能值得研究。
      猜你喜欢
      • 1970-01-01
      • 2020-03-10
      • 2015-05-16
      • 2020-06-03
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 2013-07-21
      相关资源
      最近更新 更多