Login redirects to action instead of header

Asked

Viewed 43 times

2

Hello, I am making a login page but it is not redirecting the way I would like, using the header, instead when I click in it goes to the form action. The problem file is this one(postlogin.php):


<?php

    session_start();
    

    if(isset($_POST['login'])){
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    
    if(strlen($email) > 0 && strlen($senha) > 0) {
    
    $conn = mysqli_connect("localhost", "root", "", "projeto_morada");
    
    
    //Execução da instrução sql
    $result = $conn->query("SELECT * from usuario where email = '$email' AND senha = '$senha';");
    
    
    //$usuarios recebe a lista de usuários 
    
    $usuarios = mysqli_fetch_all($result);
    
    $_SESSION['nome'] = $usuarios[0][1];
    $_SESSION['email'] = $usuarios[0][2];
    $_SESSION['senha'] = $usuarios[0][3];
    
    header('Location: http://localhost/projeto_morada/index.php');
    }
    else {
    
    echo "
    
    <script>
    alert('E-mail ou senha inválidos!')
    location.href = 'index.php'
    </script>
    ";
    
    }
};
    ?>

And the form looks like this:

          <form class="form" method="post" action="postlogin.php">

                    <label class="label-input" for="email">
                        <i class="far fa-envelope icon-modify"></i>
                        <input type="email"  id="email" onkeyup="valida_login()" placeholder="E-mail">
                    </label>

                    <label class="label-input" for="senha">
                        <i class="fas fa-lock icon-modify"></i>
                        <input type="password" id="senha" onkeyup="valida_login()" placeholder="Senha">
                    </label>

                    <a class="password" href="#">Esqueceu sua senha?
                    </a>
                    <button class="btn btn-second" id="login">entre</button>
  • In case the first file would be the postlogin.php, correct?

  • yes, that’s right

1 answer

2


The problem is in the form, the input tags are without the attribute name, add the attribute in the input tags name so that php can receive in action for example: Input email <input type="email" id="email" onkeyup="valida_login()" placeholder="E-mail" name="email"> Password <input type="password" id="senha" onkeyup="valida_login()" placeholder="Senha" name="senha">

  • Putz, that was really very thank you!!

  • kkjj sometimes a very simple detail ends up causing big confusion. Puts the answer as right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.