简体   繁体   中英

html form not displaying values

Scenario: I have this simple HTML 5 Form inside the PHP 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>

';


?>

The form displays correctly but I can not get the value of $email, it just displays as raw text.

So, how to echo a php variable from a form inside a PHP tag.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM