Submit query on the same page of the form

Asked

Viewed 212 times

0

I’m running a web application for a "Shared Races" (Uber type) service, where I want a CPF query to be presented on the green card. I want when I see the CPF, present on the green card, without leaving the page.

inserir a descrição da imagem aqui

Form script:

    <form id="cons_motoca" action="componentes/cons_motoca.php" method="POST">
        <div class="input-field col s10">
            <input placeholder="Busca por CPF" name="cpf" type="text" class="validate">
        </div>
        <div class="input-field col s2 ">
            <button class="btn-floating btn-large waves-effect waves-light " type="submit" name="consulta-completa"><i class="material-icons">search</i></button>
        </div>
    </form>

Script of the bank query:

<div>
<?php

    if(isset($_POST['cpf'])){
            echo "<table>";
            echo "<tr>";
                echo "<th>NOME</th>";
                echo "<th>DISPONIBILIDADE</th>";
                echo "<th>CARRO</th>";
                echo "<th>CPF</th>";
                echo "<th>DATA DE NASCIMENTO</th>";
                echo "<th>SEXO</th>";
            echo "</tr>";

            $strcon = mysqli_connect('localhost', 'root', '') or die('Erro ao conectar ao banco de dados');
            mysqli_select_db($strcon, 'uber');
            $sql = "SELECT nome, disponibilidade, carro, cpf, nascimento, sexo FROM tb_motorista WHERE cpf = $_POST[cpf]";
            $resultado = mysqli_query($strcon,$sql) or die("Erro ao retornar dados");

            while ($registro = mysqli_fetch_array($resultado)) {
                $nome = $registro['nome'];
                $disponibilidade = $registro['disponibilidade'];
                $carro = $registro['carro'];
                $cpf = $registro['cpf'];
                $nascimento = $registro['nascimento'];
                $sexo = $registro['sexo'];
                echo "<tr>";
                    echo "<td>".$nome."</td>";
                    echo "<td>".$disponibilidade."</td>";
                    echo "<td>".$carro."</td>";
                    echo "<td>".$cpf."</td>";
                    echo "<td>".$nascimento."</td>";
                    echo "<td>".$sexo."</td>";
                echo "</tr>";
            }
            mysqli_close($strcon);
        echo "</table>";

    } else {
        echo 'Digite o CPF de um Motorista para realizar uma consulta';
    }
?>

  • research on AJAX

  • Try with the jQuery.ajax(). The official link already has some examples

  • You can use ajax or Axios to make the request asynchronously. I even prefer Axios (https://github.com/axios/axios).

No answers

Browser other questions tagged

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