1

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First Form</title>
</head>
<body>
    <form action="form.php" method="get">
       Name: <input type="text" name="username">
        <br>
       Age: <input type="number" name="age"><br>
        <input type="submit">
    </form>

    <?php 
    echo $_GET["username"];
    echo "</br>";
    echo $_GET['age'];
    ?>
</body>
</html>

I'm getting an error:

Warning: Undefined array key "username" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 18

Warning: Undefined array key "age" in C:\Users\nambi\Desktop\PHP Tutorial\form.php on line 20.

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
Nambi Rajan
  • 23
  • 1
  • 1
  • 3

1 Answers1

4

You should check first if the array key-value exists before you echo, like this:

<?php
if (isset($_GET["username"], $_GET["age"])) {
    echo $_GET["username"];
    echo "</br>";
    echo $_GET['age'];
}
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58