Fill two inputs automatically, using data from an input and select searched in database

Asked

Viewed 1,056 times

-2

I have a problem, I was developing in PHP that I already have some knowledge. But a client asked me to make a system for him and I took something that was kind of unusual for me. The client needs that in his registration form, the user who is registering, select the state where he lives and enter the voter title, when typing search for this information in the database (Mysql) and return the user’s name and city automatically or send an Alert to the user if it is not found. Is it possible to do this? What language would it use? Would you use client language, like javascript? What would be an idea to develop this? Note: I have always been more focused on desktop programming language than I have good web knowledge and only PHP.

  • If you use jQuery, just put inside the file that Ajax will call a script filling the fields NAME and CITY if they are found; if not, send an Alert.

1 answer

-1


To do this use Javascript, or better yet, a javascript library called j-query, that greatly facilitates the requisitions Ajax, example...

form

# Jquery #
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 

# Requisição ajax #
<script src="model/ajaxCli.js"></script>

<form name="formCurl" id="env" method="post">

    <label for="inpPesquisa">Pesquise por clientes</label>
    <input type="text" placeholder="Digite o nome" name="termo" required="required">

    <input type="submit">

</form>

ajaxCli.js

$(document).ready(function(){


        $( "#env" ).submit(function( event ) {
         event.preventDefault();

         # pega os dados do formulário #
         var data = $("#env").serialize();

            $.ajax({
                type : "POST",
                # página php onde irá consultar #
                url  : "controller/clientes.php",
                data : data,
                dataType: "json",
                success : function(response){
                    # em caso de sucesso #
                    if (response.codigo == 1) {

          $('tbody').html('<tr><td>' + response.nomeCli + '</td><td>' + response.nomeCat + '</td></tr>');

                    };

                    # em caso de cliente não encontrado #
                    if (response.codigo == 0) {

          alert('Cliente não encontrado');

                    };
                }
            })
        });

    })

php clients.

<?php
   class getClientes{

    # seu método # ...

if(#sua condição#) {
      $retorno = array('codigo' => 1);
        echo json_encode($retorno);
        exit();
    }
    else {
      $retorno = array('codigo' => 0);
      echo json_encode($retorno);
      exit();
  }

   }
   $execClass = new getClientes();
   $execClass->termo = (isset($_POST['termo']) ? $_POST['termo'] : '');
   $execClass->_getClientes($execClass->termo);
?>

Basically that’s it, just study this function more. By the way, since it comes from desktop, you can change the library J-query by a more modularized, example AngularJs, more this for medium/long term projects, because the learning curve is higher

Browser other questions tagged

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