Fill in two inputs automatically, using input and select data from a bank

0

I'm having a problem, I was developing in PHP that I already have some knowledge. But a customer asked me to make a system for him and got something that was kind of unusual for me. The client needs that in the registration form, the user that is registering, select the state where he lives and enter the title of voter, when enter search this information in the database (MySql) and return the name and the city of the user automatically or send an alert to the user is not found. Is it possible to do this? What language would be used? Would you use client language, javascript type? What would be an idea to develop this? Note: I've always been more focused on the language of programming the language that I have a good web knowledge and only PHP.

    
asked by anonymous 31.07.2017 / 03:21

1 answer

0

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

form

# Jquery #
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>#Requisiçãoajax#<scriptsrc="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');

                    };
                }
            })
        });

    })

clients.php

<?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 the desktop, you can change the J-query library to a more modularized one, example AngularJs , plus this for medium / long term projects, since the learning curve is bigger

    
31.07.2017 / 04:23