Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Little problem with HTML form and input tags and with PHP get method (Warning: Undefined array key)

I am trying to get some basic information from the user –

<body>
  <form action = "index.php" method="get">
    Name: <input type="text" name="username">
    <br>
    Age: <input type="number" name="age">
    <input type="submit">
  </form>
  <br>
  Your name is <?php echo $_GET["username"] ?>
  <br>
  Your age is <?php echo $_GET["age"] ?>
 </body>
  • this code is functional, but when I look to the browser prior to filling in the name and age, there is kind of a glitch, which can be seen underneath the submit button.
Your name is
Warning: Undefined array key "username" in path\index.php on line 15

Your age is
Warning: Undefined array key "age" in path \index.php on line 17

However, when I enter in some information, then that error disappears, but it would be nice, if it was not there in the first place.

Could anybody please give me any advice on how to fix this bug? Thank You very much for Your eventual efforts.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Wrap your echo under isset() condition. It will prevent the execution of the code if $_GET["username"] is not set or in other words, it will prevent the display if the form is not submitted(like on page load)

if(isset($_GET["username"])){
   echo $_GET["username"];
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading