简体   繁体   中英

PHP inside of HTML inside of PHP

How would I go about doing this?

<?php
if (isset ($_SESSION['ID'])) {
    echo " 
         <form action = 'updateacct.php' method = 'POST'>
              Email: 
                    <input type = 'text' name = 'eml' value = '" . echo $_SESSION['ID'] . "' placeholder = 'Email' size = '30' required/>
         </form>

?>

I'm trying to pull a var from the session and put it inside a form value and can't figure out how to do so.

It's not recommended to echo your whole html in PHP... You could do it like this:

<?php if(isset($_SESSION['ID'])): ?>
    <form action='updateacct.php' method='POST'>
        Email: <input type='text' name='eml' value='<?php echo $_SESSION['id']; ?>' placeholder='Email' size='30' required/>
    </form>
<?php endif; ?>

No need for the second echo. You are already echoing.

I took your code and simplified it a bit. I use multiple echos to make it clearer what we do.

<?php
if (isset($_SESSION['ID'])) {
    echo '<form action="updateacct.php" method="POST">';
    echo '    Email:';
    echo '    <input type="text" name="eml" value="' . $_SESSION['ID'] . '" placeholder="Email" size="30" required />';
    echo '</form>';
}
?>

I would go like this:

<?php if (isset ($_SESSION['ID'])) : ?>
     <form action = 'updateacct.php' method = 'POST'>
          Email: 
                <input type = 'text' name = 'eml' value = '<?= $_SESSION['ID'] ?>' placeholder = 'Email' size = '30' required/>
     </form>

<?php endif; ?>

You can say:

<?php
if (isset ($_SESSION['ID'])) {
?>

// HTML goes here

<?php
}
?>

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