Is using Jquery on a PHP page possible?

Asked

Viewed 478 times

1

I have a login page, in which there is a form, which sends the data of two fields (username and password) to a PHP file that checks. This php file uses a function already written in a class. In this function, if the result is true, a redirect happens, if it is not, it will execute a Jquery code to display an "Invalid Password" message there in the login page. The intention is that this Jquery changes the property of a certain element from "Hidden" to "Visible", thus presenting the message on the screen.

I’m a little confused, and I’ve never really got it right with jQuery, and I don’t know if I’m missing any important details, so I’d like a good soul to give me a hand. Follows the codes HTML:

<?php
header("Content-Type: text/html; charset=ISO-8859-1", true);
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title>Login</title>
        <meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" /> 
        <link rel="stylesheet" type="text/css" href="css/login_style.css">
    </head>
    <body>
        <div class="login-page">
            <div class="form">
                <form class="login-form" action="verificacaologin.php" method="post">
                    <input type="text" placeholder="Usuário" name="nome_usuario"/>
                    <input type="password" placeholder="Senha" name="senha"/>
                    <button type="submit">login</button>
                </form>
                <label class="senha_incorreta">Senha incorreta.Tente novamente.</label>
            </div>
        </div>
    </body>
</html>

Php doing the checking:

<?php
include_once 'classes/Usuario.php';

$nome = $_POST['nome_usuario'];
$senh = $_POST['senha'];

$usuario = new Usuario();

$usuario->verificarExistenciaLogin( $nome , $senh);
?>

Function of the User class containing Jquery:

public function verificarSenhaCorreta( $nome_usuario , $senha )
{
    $query = "select SENHA from portfolio.usuario where NOME_USUARIO = '$nome_usuario' ";
    $conexao = new Conexao();
    $resultado_consulta = $conexao->abrirConexao( $query );

    $resultado_consulta = implode($resultado_consulta);

    if( $resultado_consulta == $senha )
    { 

          header("Location:ordem_servico.php");
    }
    else
    {
        //mostrar mensagem de que a senha está incorreta();

        ?>
        //Aqui que minhas dúvidas começam
        <script>

        $(document).ready(function()
        {
            $('.senha_incorreta').css( "visibility" , "visible" );
        });

        </script>
        <?php
    }
}
  • 1

    There is no "PHP page". PHP can even generate a page, but after it is sent to the client it is the same as any other. However, since in PHP you have control over what will be sent, it makes much more sense to send the correct information to the user. EVENTUALLY you can load the data via AJAX or any other interactive method, but this does not matter what technique you will use in JS (if it is pure JS, if it is jQuery, or whatever you choose)..

  • 1

    PHP is a script that, in addition to other features, generates HTML that in turn can import or have Javascript

  • From what I saw in your code you didn’t reference the file Jquery within the HTML. I believe that when you do this will work, otherwise, try to consult through the event onclick() of button using the Ajax which will await the response of PHP and thus enable or disable the label.

2 answers

1

Good morning. Yes it is possible. Maybe this can help you:

//Codigo PHP
public function verificarSenhaCorreta( $nome_usuario , $senha )
{
  $query = "select SENHA from portfolio.usuario where NOME_USUARIO = '$nome_usuario' ";
  $conexao = new Conexao();
  $resultado_consulta = $conexao->abrirConexao( $query );

  $resultado_consulta = implode($resultado_consulta);

  if( $resultado_consulta == $senha )
  { 

      header("Location:ordem_servico.php");
  }
  else
  {
    //mostrar mensagem de que a senha está incorreta();
    echo "<script>functionHideMessage();</script>"
  }
}

//Codigo HTML, faca um 'style display:none' pra ocultar <label>
<label class="senha_incorreta" style="display:none;">Senha incorreta.Tente novamente.</label>

//Codigo Jquery fora do PHP
//Se lembra de chamar a libreria Jquery
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
       function functionHideMessage(){
          //Com o '.show()', removemos a propiedade 'display:none' e o conteudo do <label> fica visível
          $('.senha_incorreta').show();
       }           
    });
</script>
  • 2

    Tell you what?

  • All right, I just wanted to know if the answer was helpful to him or if I could help him with something else.

  • So I’ll give you a hint, don’t take it as a negative review. For your answer to be useful to other users it would be nice for you to describe what you did to solve the problem and why you think the error was happening. It gets much more didactic

  • Okay, all right. Thanks for the tip.

0

Create a function outside of PHP, along with your php, in this template:

<body>
    ... conteudos php, html e etc
    <script>
        function exibeMensagemErro() {
            $('.senha_incorreta').css( "visibility" , "visible" );
        }
    </script>
</body>

and in your life, do:

echo "<script>exibeMensagemErro()</script>";

Browser other questions tagged

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