Baker2795
Baker2795

Reputation: 247

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.

Upvotes: 0

Views: 80

Answers (4)

Chin Leung
Chin Leung

Reputation: 14941

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

Upvotes: 5

Kamrul Khan
Kamrul Khan

Reputation: 3360

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

Upvotes: 1

Amr Aly
Amr Aly

Reputation: 3905

You can say:

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

// HTML goes here

<?php
}
?>

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

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>';
}
?>

Upvotes: 2

Related Questions