0

I'm making a very basic login page with HTML & PHP. However, when I press the login button, a

Notice: Undefined index:mail

shows up. As you can see in the code, I've used an if (isset($_POST['submit'])) so I can't imagine that being the problem. Can someone tell me what is wrong with my approach?

HTML

<form id="login_form" action="" method="post">

            <label for="mail">E-mailadres*:</label>
            <input id="mail" type="text"/> <br/> <br/>

            <label for="password">Wachtwoord*:</label>
            <input id="password" type="password"/> <br/> <br/>


            <label for="submit"></label>
            <input id="submit" type="submit" name="submit" value="Inloggen"/> <br/> <br/>
        </form>

        <?php
        if (isset($_POST['submit'])) {
            include 'php/login.php';
        }
        ?>

PHP

<?php
include('connect.php');

if (isset($_POST['submit'])) {
    if ($_POST['mail'] == '' || $_POST['password'] == '') {
        echo "Vul alle velden in.";
    } else {

        $user_mail =$_POST['mail'];
        $user_password=$_POST['password'];

        $user_mail = stripslashes($user_mail);
        $user_password = stripslashes($user_password);

        $user_mail = mysql_real_escape_string($user_mail);
        $user_password = mysql_real_escape_string($user_password);

        $sql="SELECT * FROM floro WHERE email='$user_mail' and wachtwoord='$user_password'";
        $res = mysql_query($sql);
        if ($res) {
            ?>
            <div id="formResult"><?php echo 'U bent ingelogd'; ?></div>
        <?php
        } else {
            ?>
            <div id="formResult"><?php echo "De gegevens zijn onjuist."; ?></div>
        <?php
        }

    }
}
?>
j08691
  • 204,283
  • 31
  • 260
  • 272
user3182261
  • 271
  • 2
  • 10
  • 21

2 Answers2

4

Your inputs should have a name, not just an id.

So this will not work:

 <input id="mail" type="text"/> <br/> <br/>

But this will

 <input name="mail" type="text"/> <br/> <br/>

And if required by javascript, you could use:

 <input name="mail" id="mail" type="text"/> <br/> <br/>

Just remember, the server wil only and only receive form elements that have a name="..." element.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Flip Vernooij
  • 889
  • 6
  • 15
2

change

<label for="mail">E-mailadres*:</label>
<input id="mail" type="text"/> <br/> <br/>

<label for="password">Wachtwoord*:</label>
<input id="password" type="password"/> <br/> <br/>

to

<label for="mail">E-mailadres*:</label>
<input id="mail" name="mail" type="text"/> <br/> <br/>

 <label for="password">Wachtwoord*:</label>
 <input id="password" name = "password" type="password"/> <br/> <br/>

in your form.

fiction
  • 566
  • 1
  • 6
  • 21
  • *Food for thought:* Giving the OP an added explanation would have been beneficial, including a *probable* higher rate of upvotes. It's all about learning ;) – Funk Forty Niner Aug 04 '14 at 20:05
  • I think it's better to OP to understand so easy problem for his own, if he need it. – fiction Aug 04 '14 at 20:11
  • *"I think it's better to OP to understand so easy problem for his own, if he need it."* No, I disagree. Stack is not only about fixing code, it's about informing them as to why it failed. If someone else that visits this question and doesn't understand why it failed, then that person would have learned nothing. You know, I know, yet don't take it for granted that everyone else knows. ;) – Funk Forty Niner Aug 04 '14 at 20:41
  • Ok, will note it next time. Thank you. – fiction Aug 04 '14 at 20:42
  • I completely understand what the problem was mate ;) Just forgot to add a name and when you don't think about it the first time writing the code, you're not gonna see it when checking your code. I don't at least. – user3182261 Aug 06 '14 at 13:13