0

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.

4
  • 1
    I suggest you start reading the documentation of the tools you want to use: php.net/manual/en/language.types.string.php Commented Feb 13, 2017 at 21:18
  • 2
    Firstly, you have an obvious syntax error for what you posted; edit: two actually, possibly three. Commented Feb 13, 2017 at 21:29
  • @Fred-ii- okay thanks for the help Commented Feb 13, 2017 at 22:04
  • you're welcome. Had it been just that, I'd of submitted an answer right away, soon as it opened. Pretty sure all that was wrong was a missing quote/semi-colon and brace. Commented Feb 13, 2017 at 22:30

4 Answers 4

5

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

0

You can say:

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

// HTML goes here

<?php
}
?>

1 Comment

This doesn't really answer my question but helped clean up my code so thanks for that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.