I wanted to put an alert on my login screen PHP, JS, HTML, MYSQL

1

I wanted to put an alert on my login screen, if the registration and password correspond to what is in the bank direct pass, if not, an alert appeared or the box would turn red with the error with something like "Enroll or Wrong Password"

here is 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 template

<?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 YOU CAN HELP ME.

    
asked by anonymous 05.10.2018 / 22:43

2 answers

0

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

    
05.10.2018 / 23:04
0

I do not know very well how MVC works and I do not know if it is the best way to do this, but I hope it can help you:
$query ="SELECT count (*) FROM WHERE user WHERE enrollment =: enrollment and password =: password ", to know if there is any user with this registration and password, soon you will get 0 or 1 as a reply, since the registration number is unique. And instead of using fetchAll (), you can use fetchColumn () (Here's the usage -> link ).

Now I'm in doubt if 0 or 1 will get you in the model or the controller. Assuming it's in the controller, you can use an allowed or denied access condition for the view. In the view, I do not 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 do not understand much about MVC, but I hope this process helps you.

    
06.10.2018 / 00:33