Fields fill [closed]

0

I have a form in which, when entering a license plate in the registration field, it lists the license plates. And each enrollment is related to a person and their data: name, date of birth, date of admission.

However, I need when I click on the registration, it will fill in the other fields on the form. And this part I'm not able to elaborate and execute.

This is the code that lists the registrations:

<?php
$host="localhost"; // Host name
$username="teste_inezb"; // Mysql username
$password=""; // Mysql password
$db_name="teste_login"; // Database name


    $con = mysql_connect($host,$username,$password)   or die(mysql_error());
    mysql_select_db($db_name, $con)  or die(mysql_error());

$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "select DISTINCT MATRIC from DBWEBCAD where MATRIC LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $cmat = $rs['MATRIC'];
    $cname = $rs ['NOMSCODEP'];
    echo "$cmat\n", "$cname\n";
}
?>
    
asked by anonymous 13.01.2017 / 18:57

1 answer

3

I can give you such a path. Imagine that you have the HTML fields on the page and the enrollment list, with a class called numero_matricula .

HTML

<li id="mat123" class="numero_matricula"> 123 </li>
<li id="mat231" class="numero_matricula"> 231 </li>
<li id="mat321" class="numero_matricula"> 321 </li>

<input type="text" id="nome">
<input type="text" id="idade">
<input type="text" id="email">

When you click on a number, the class will call the function below that will call a page through AJAX that will search the data in PHP.

JS - jQuery

$(document).on('click', 'li.numero_matricula', function(){
   var matricula = $(this).attr('id').replace('mat', '');
   $.ajax({
      type: POST,
      url: 'pagina.php',
      data: { mat: mat },
      success: function(result){
         $('#nome').val(result.nome);
         $('#idade').val(result.idade);
         $('#email').val(result.email);
      }
   })
});

The search page is this. Quite simple, a SELECT searching the user data for the number of the registration coming by POST of the function AJAX above. The result is converted to JSON and imputed in the fields.

PHP

pagina.php

$mat = $_POST['mat'];
$query = "SELECT * FROM users WHERE numero_matricula = $mat";
$arrDados = json_encode(mysql_fetch_array($query));
return $arrDados;
    
13.01.2017 / 20:01