Field filling

Asked

Viewed 66 times

0

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

However, I need that when I click on the registration, it fill in the other fields of the form. And that part I’m not getting 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";
}
?>
  • Do you want to change pages to do this or stay the same ?

  • Anyway. The registration is already listed, I just need it to complete the required fields.

  • I’m not quite able to understand what your problem is which difficulties you are facing. You could be more specific?

  • I edited the question

  • @Inezboldrin you will have to create another function that by clicking on the registration, fill in the fields. This can be done with JS, AJAX.

  • Good afternoon @Inezboldrin, a recommendation outside the scope of your question: As your code stands, it is vulnerable to SQL Injection. So much so that this function mysql_query was depreciated by PHP. I suggest using other alternatives such as Mysqli or PDO, and use concepts such as prepare statements, for example.

  • Regarding your question, I believe you want to make a request to the server after the page loaded, to search for a data that arose from the interaction with the user (in this case, the registration). This? This process in javascript is called AJAX, and involves creating a client-side script in javacript and a PHP script to return this data (in the form of JSON, for example). Anyway, you would have to show us your current HTML/JS implementation to better understand your problem. Hugs.

  • So, I did some research and saw about Ajax, JSON however, I don’t know how to elaborate a code with them. You have some more explanatory video?

  • @Inezboldrin you already know something of JavaScript? If not, I recommend learning the basics of this language. Afterwards, you can study jQuery, a well-known library that makes life easier for those who work with js (in general, works on javascript, but makes it easier - especially for those who are starting and do not want to delve into pure javascript -, and more compatible your code - you do not need to write different things for other browsers). Inside jQuery there are methods to get what you want here ($.ajax).

  • As for classes, I’m not really into it. But I recommend looking on Youtube, there are a lot of good people there.

Show 5 more comments

1 answer

3

I can give you a path like this. Imagine you have the HTML fields on the page and the list of registrations, 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">

By clicking on any registration the class will call the function below which will call a page by AJAX which 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 page that does the search is this. Very simple, a SELECTsearching user data by registration number coming by POST of function AJAX above. The result is converted into 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;

Browser other questions tagged

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