0

I'm totally a beginner at PHP and get a probably silly error there. Any inputs are welcome.

I'm trying to retrieve data from an input box using the global POST but it can't find the input object. Could you please advise?

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php

?>

<form action="post.php" method="post">
    <input type="text" name="name1">
    <input type="text" name="age">
    <button type="submit">SUBMIT</button>
</form>

<?php
//POST
//Collect data from the HTML form (invisible)

if (isset($_POST)) {
    echo $_POST['name1'] . ", your form is submitted";
}

echo"<br>";
print_r($_POST);

?>

</body>
</html>

Error Message: "Warning: Undefined array key "name1" in C:\xampp\htdocs\PHP_Course\Operators\post.php on line 22 , your form is submitted Array ( )"

Many thanks!!

Raul Biondo
  • 17
  • 1
  • 8
  • 1
    Is the code you've shown `post.php` (meaning the form submits to this same page)? – El_Vanja Jan 13 '21 at 17:14
  • Is the file you posted above called "post.php"? – SteJ Jan 13 '21 at 17:14
  • What does `$_POST` output? – M. Eriksson Jan 13 '21 at 17:14
  • 2
    `if (isset($_POST)) {` will always evaluate as true. If nothing was posted it will always be an empty array. `isset()` only checks if the variable is defined (which it is by default) and not `null`. You should always check if each param is set `if (isset($_POST['name1'], $_POST['age']))` – M. Eriksson Jan 13 '21 at 17:15

0 Answers0