-1

I'm started to learn PHP only some weeks ago, and I have a problem with posting a value from form. Code:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" 
method="POST">
            <input type="text" name="name">
            <br/>
            <input type="submit">
        </form>
        <?php 
            echo $_POST['name'];
        ?>
    </body>
</html>

It displays an error But when I try to write something in the input field, it is working perfectly

  • 1
    Note `action=""` is completely unncesessary almost always, if you just don't include the `action=` part at all, it will by default post back to the same page. – GrumpyCrouton Sep 27 '18 at 17:48

1 Answers1

1

That's because before you post the form you're trying to print $_POST['name'] which doesn't exist yet. If you make a check before printing it it should remove the warning

if (isset($_POST['name'])) {
    echo $_POST['name'];
}
Johan
  • 3,577
  • 1
  • 14
  • 28