Skip to content
Advertisement

How can I go about removing the error message “Undefined Index” for $_POST[“username”], when it will be undefined until the form is submitted?

How do I get rid of the error message at the start of this code? I recognize and understand why the message is popping up, but I’m either not being clever enough, need to go to sleep, or both, as I can’t figure out what the best way to remove the error is.

The error in question: Undefined index: username in C:xampphtdocsloginlogin.php on line 4

Until the $_POST[“username”] variable is defined when you hit “login” it stays at the top of the page. How can I go about repairing this code?

Here is the GitHub full code: https://github.com/TakingJester766/PHPLogin/blob/main/login.php

Advertisement

Answer

You shouldn’t access $_POST until it’s actually defined. To do that you can do something like:

if (isset($_POST["username"])) {
  $logged_user = $_POST["username"];
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement