Redirect from Login in html

3

I'm trying to make a login in html and validate if the username and password are correct with a javascript (for the moment I only see if username and password are the same), and depending on whether they are correct or not redirect me to one page or another, the problem is that I have not managed to make a script that works for me, the data entry I have it in a form that looks like this:

<div class="container">
  <form method="POST" name="formLogin" action="logeado.html" onsubmit="login(userName,password)">
          USERNAME<br>
          <input type="text" name="userName" placeholder="Nombre de usuario"><br>
          PASSWORD<br>
          <input type="text" name="password" placeholder="Contraseña">
          <br>
          <input type="submit" name="btnLogin" value="Ingresar" onsubmit="login">
   </form>
</div>

It's something simple but I just want to make it work, the issue is that if username and password are the same I want to redirect to "logeado.html", otherwise I want to stay in the current page that is "login. html "and send me an error alert. I am using visual code with html5. I hope you can help me, thank you very much!

    
asked by Roberto Antonio Rico Palma 31.03.2018 в 19:46
source

1 answer

3
  

Depends on the method you use but the logic would be the same: Suppose   what do you use php:

<?php require_once('conn.php'); // aca se requiere el archivo de conexion

    // aca verificamos que las variables post tengan algun tipo de contenido
    if (isset($_POST['userName']) AND isset($_POST['password'])) {
        // realizamos la consulta a la base de datos y pedimos que nos seleccione todo de la tabla que conetnga tanto el usuario O el password
        $sql = "SELECT * FROM USUARIOS WHERE usuario = '".$_POST['userName']."' OR password = '".$_POST['password']."'";
        $con = mysqli_query($conx, $sql);
        $ls = mysqli_fetch_assoc($con); 

        // VALIDACIONES
        if ($_POST['userName'] == $ls['usuario'] AND $_POST['password'] == $ls['contra']) {
            //si usuario que viene del formulario es igual al usuario que traemos de la consulta son iguales Y password del formulario es igual al password de la tabla 
            // si los datos son correctos iniciamos seccion y lo dirigimos la pagina objetivo
            session_start();
            header('location: paginaobjetivo.html');

        }elseif ($_POST['userName'] != $ls['usuario'] AND $_POST['password'] == $ls['contra']) {
            // si el suario del formulario NO es igual al de la tabla Y password del formulario es igual al password de la tabla 
            echo "Tiene un error en el usuario verifique la informacion";
        }elseif ($_POST['userName'] == $ls['usuario'] AND $_POST['password'] != $ls['contra']) {
            // si el suario del formulario es igual al de la tabla Y password del formulario NO es igual al password de la tabla 
            echo "El password es erroneo verifique la informacion";
        }

    }
?>

This can follow you as an example to do it.

    
answered by 31.03.2018 / 22:02
source