-1

I am new to php and I have a little problem with Undefined index.

Notice: Undefined index: meno in C:\xampp\htdocs\index.php on line 20

Notice: Undefined index: heslo in C:\xampp\htdocs\index.php on line 21

My code:

<!DOCTYPE html>
<html>
<head>
    <title>Prihlásenie sa na stránku</title>
</head>
<h1>
    <center>
        Prihlásenie
    </center>
</h1>
<body>
    <form action="prihlasenie.php" method="post">
        <P>Meno: <input type="text" name="meno"></input></P>
        <P>Heslo: <input type="password" name="heslo"></input></P>
        <P><input type="submit" value="prihlasit"></input></P>
    </form>
    
    <?php 

        $uname = $_POST["meno"];
        $passwd = $_POST["heslo"];

        if ((empty($uname)) or (empty($passwd)))
        {
            echo "Vyplnte meno a heslo!";
        }
    ?>

</body>
</html>

Thx for response (sorry for my english)

Community
  • 1
  • 1
Nezo
  • 98
  • 9
  • Check if the `POST` is populated before trying to use it. Also the `h1` should be in the `body`. – chris85 Sep 24 '17 at 16:31

1 Answers1

0

you didn't check whether form submitted or not, so you code should be as follows

<?php 
if(isset($_POST["meno"])) {
    $uname = $_POST["meno"];
    $passwd = $_POST["heslo"];

    if ((empty($uname)) or (empty($passwd)))
    {
        echo "Vyplnte meno a heslo!";
    }
}
    ?>
user1844933
  • 3,296
  • 2
  • 25
  • 42
  • Ok now there is no error but when i press "prihlasit" whitch means "submit" it will redirect me to "prihlasenie.php" no mather if "meno" or "heslo" is empty or not. – Nezo Sep 24 '17 at 16:35
  • @Nezo What did you intend to happen, `action="prihlasenie.php"`? PHP is server side, are you looking for a client side check perhaps? – chris85 Sep 24 '17 at 16:52