I wanted to put an Alert in my login screen PHP, JS, HTML, MYSQL

Asked

Viewed 335 times

1

I wanted to put an Alert on my login screen, if the registration and password match what’s in the bank pass straight, if not, an Alert appeared or the boxes were red with the error with something like "Wrong registration or password"

There’s the connection to my bank

<?php 
$username = 'root';
$password = '';
$connection = new PDO( 'mysql:host=localhost;dbname=nise', $username, $password );
?>

My Controller

<?php 
include 'usuarios/modelUsuario.php';

//se clicou no botão
if (isset($_POST['login'])){

    $modelo = new ModelUsuario();


    $resultado = $modelo->autenticar( ($_POST['matricula']), ($_POST['senha']) );
    if(! empty($resultado) ){
      // redirecionar para index2
      header("location: http://localhost/projeto/aluno.php"); 
    }

}

?>

The model

<?php
include 'usuarios/usuario.php';

class ModelUsuario{

    public function adicionar(){
        include 'usuarios/bd.php';

        $query = "INSERT INTO usuarios (matricula, senha) 
              VALUES (:matricula, :senha)";

        $statement = $connection->prepare($query);

        $valores = array();
        $valores[':matricula'] = $usuario->getMatricula();
        $valores[':senha'] = $usuario->getSenha();


        $result = $statement->execute($valores);


    }

    public function editar(){

    }

    public function remover(){

    }


   public function autenticar($matricula, $senha){
        include 'usuarios/bd.php';


        $query = "SELECT nome, id FROM usuario WHERE matricula=:matricula and senha=:senha";


        $statement = $connection->prepare($query);

        $valores = array();
        $valores[':matricula'] = $matricula;
        $valores[':senha'] = $senha;


        $result = $statement->execute($valores);


        $result = $statement->fetchAll();



        return $result;

    }




}

?>

And my form

 <form  method="POST" action ="login.php" name="for">
            <div class="form-group">
              <div class="form-label-group">
                <input type="text" id="inputEmail" name="matricula" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
                <label for="inputEmail">Matrícula</label>
              </div>
            </div>
            <div class="form-group">
              <div class="form-label-group">
                <input type="password" id="inputPassword"   name="senha" class="form-control" placeholder="Password" required="required">
                <label for="inputPassword">Senha</label>
              </div>
            </div>
            <div class="form-group">
              <div class="checkbox">
                <label>
                  <input type="checkbox" value="remember-me">
                  Lembrar senha
                </label>
              </div>
            </div>
                  <button type="submit" class="btn btn-primary btn-block" href="aluno.php" name="login">Login</button> 
                    <a href="registro.php" id="cadastro" class="d-block small mt-3">Cadastrar</a>
          </form>

NOTE: ALERT CAN BE IN JS OR AJAX, SO HELP ME.

2 answers

1


I don’t really know how MVC works and I don’t know if it’s the best way to do it, but I hope I can help you:
$query = "SELECT Count(*) FROM usuario WHERE matricula=:matricula and password=:password", to know if there is any user with this registration and password, soon you will get 0 or 1 as a response, since the registration number is unique. And instead of using fetchAll(), you can use fetchColumn() (Here the use of the same -> http://php.net/manual/en/pdostatement.fetchcolumn.php).

Now I’m wondering if 0 or 1 will get you in the model or controller. Assuming it is in the controller, you can use an access condition that is allowed or denied for the view. In the view, I don’t know if you’re requesting via ajax, but if you’re using something like:

$.post('controller.php', function (acesso){

 if(acesso === 'permitido')
 {

    window.location.href = 'aluno.php';

 }

 if(acesso === 'negado')
 {

    alert('acesso negado');  

 }

});

As I said, I don’t know much about MVC, but I hope this process helps you.

0

You can give a echo "<script> alert("texto aqui") </script>"; however, I imagine you want to make a modal, Alerts do not work as well, especially on mobile devices. Take a look at here

Browser other questions tagged

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