Mysql query with AJAX

Asked

Viewed 50 times

0

I created a table "usuario" in the MySQL containing name and password. I have established a connection with the PHP and I would like to consult on AJAX, to validate this name and password and print a message saying if they are valid or not. But I do not know how to do the part of the query in the inputs.

<html>
<head>
    <title>Exercício Tec Prog I</title>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {

            $("#bEntrar").click(function(){
                fLogar();


            });

            function fLogar()
            {
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: "usuario.php",
                    success:function(dados)
                    {
            }   

        });         
    </script>
</head>
<body>

    <form action="usuario.php" method="POST">
    <label>Usuario</label>
    <input type="text" id="tLogin"/>
    <label>Senha</label>
    <input type="text" id="tSenha">
    <button id="bEntrar">Entrar</button>
    <div id="divFilmes"></div>
</form>

</body>
</html>


     <?php



 $tLogin = $_POST["tLogin"];
 $tSenha = $_POST["tSenha"];

    //Conectando ao banco de dados
 $con = new mysqli("localhost", "root", "root", "tecprog");
 if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

    //Consultando banco de dados
 $qryLista = mysqli_query($con,"SELECT * FROM usuario WHERE nome = '$tLogin' AND senha = '$tSenha'");    
 while($resultado = mysqli_fetch_assoc($qryLista)){
    $vetor[] = array_map('utf8_encode', $resultado); 
 }    

    //Passando vetor em forma de json
 echo json_encode($vetor);

 ?>
  • You want to know if the user exists or not, that’s it?

  • Ué, you make the connection with OO and dps goes to procedural, masoq

1 answer

0


If it’s just to check if the user exists, just do this:

  $tLogin = $_POST["tLogin"];
  $tSenha = $_POST["tSenha"];

//Conectando ao banco de dados
$con = new mysqli("localhost", "root", "root", "tecprog");
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

 if($con->query("SELECT * FROM usuario WHERE nome ='$tLogin' AND senha = '$tSenha'")->fetch_all()){
    //Usuário existe
    echo 'Existe';
 }else{
    //Usuário não existe
     echo 'Não existe';
 }

Your inputs were without the name field, so try:

<form action="usuario.php" method="POST">
<label>Usuario</label>

<input type="text" id="tLogin" name="tLogin"/>

<label>Senha</label>

<input type="text" id="tSenha" name="tSenha">

<button id="bEntrar">Entrar</button>

<div id="divFilmes"></div>

  • In the ajax Success you decide what to do with the return

  • I tried something like that. But it is displaying the error "Undefined index" in the following lines: $tLogin = $_POST["tLogin"]; $tNew = $_POST["tNew"];

  • Ahhhh ok, the post is not going, what defines it is the name, you just put the id

  • I’ll edit here

  • @Eduardoribeiro looks there and tries, what defines the name of the post is the name field of the input, so gave giving Undefined, because you were not sending it

  • It really worked out haha, Vlw

  • @Eduardoribeiro :)

Show 2 more comments

Browser other questions tagged

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