0

I'm trying to make a functional login page using php, and I was following a tutorial I found on youtube. However when I run the code I get this error and I don't know how to fix it:

Notice: Undefined index: tEmail in C:\xampp\htdocs\PROJETOWEB\php\login.php on line 2

Notice: Undefined index: tSenha in C:\xampp\htdocs\PROJETOWEB\php\login.php on line 3

Here's the code

<?php
    $tEmail = $_POST['tEmail'];
    $tSenha = $_POST['tSenha'];
    $error = "";
    $success = "";

    if(isset($_POST['bLog'])){
        if($tEmail == "admin@cyanmail.com"){
            if($tSenha == "senha"){
                $error = "";
                $success = "Welcome";
            }
            else{
                $error = "Incorrect Password";
                $success = "";
            }
        }
        else{
            $error = "Invalid User";
            $success = "";
        }
    }
?>





<!DOCTYPE html>
<html>
    <head>
        <script src="/PROJETOWEB/js/jquery-3.4.1.js"></script>
        <script src="/PROJETOWEB/js/funcoes.js"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link rel="stylesheet" href="/PROJETOWEB/css/estilo.css">

    </head>

    <body>

        <div class="divBox">

                <div class="divBackground"></div>

                <div class="divLogo"><img src="/PROJETOWEB/img/logo.png" alt="Logo" width="10%" height="8%"></div>

                <div>
                        <h1><text class="cyan">Cyan</text>Mail</h1><br>
                        <h2>Bem vindo de volta!<br> Acesse sua conta para ver seus emails!</h2>
                </div>

                <p class="error"><?php echo $error; ?></p>
                <p class="success"><?php echo $success; ?></p>

                <div>
                    <input type="text" placeholder="E-mail" id="tEmail" class="divInput1"><br>
                    <input type="password" placeholder="Senha" id="tSenha" class="divInput2">
                </div>

                <div>
                    <button id="bLog" class="Botao">Log in</button>
                </div>

                <div class="divBotao2">
                    <button id="bSign" onclick="location.href='paginas/cadastro.html' "class="Botao2">Sign up</button>
                </div>
        </div>

    </body>

</html>

Hopefully somebody can let me know what I'm doing wrong. Thanks in advance.

Marco
  • 1
  • ok. You need a form, the form needs to use `POST` as the method and you need to test that the POST array has been dimensioned before calling the `$_POST[ var ]` statements - ie: `isset( $_POST['tEmail'] ){ etc }` – Professor Abronsius Oct 12 '19 at 14:49

1 Answers1

0

Try to use $tEmail = $_POST['tEmail']; $tSenha = $_POST['tSenha']; variable inside if(isset($_POST['bLog'])){ method.

Because if the value is not set then this error will definitely occur,

<?php

    $error = "";
    $success = "";

    if(isset($_POST['bLog'])){
       $tEmail = isset($_POST['tEmail']) ? $_POST['tEmail'] : '';
       $tSenha = isset($_POST['tSenha']) ? $_POST['tSenha'] : '';

        if($tEmail == "admin@cyanmail.com"){
            if($tSenha == "senha"){
                $error = "";
                $success = "Welcome";
            }
            else{
                $error = "Incorrect Password";
                $success = "";
            }
        }
        else{
            $error = "Invalid User";
            $success = "";
        }
    }
?>
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49