There are several form fields. You need to search for one of the fields. In this case, in the phone field, when typing more, for example, 4-5 first characters (numbers). If there are such records in the table, with a certain prefix entered by the user in the input field, then output the first 5 results and automatically fill in the remaining fields when one of the results is selected.

Here is the form handler code:

 $zakaz = $_POST['zakaz']; $tel= $_POST['phone']; $kol= $_POST['kol']; $segod = $_POST['segod']; $avto = $_POST['avto']; $stat = $_POST ['stat']; $sql = "insert into zakaz_table (zakaz, phone, kol, datavremya, member, segod, avto, stat) values ('$zakaz', '$tel', '$kol', DATE_ADD(now(), INTERVAL 0 HOUR), '$usrname', '$segod', '$avto', '$stat')"; mysql_query($sql) or die(mysql_error()); /* Закрываем соединение */ $mess = " Заказ отправлен на обратку "; //echo $sql; header("location:index.php?mess=$mess"); 

Correspondingly, zakaz is the client's address, and phone is the phone, the rest of the fields are not needed for auto-completion.

  • Any implemented code will work, then I will grab the essence - oskar
  • one
    Look in google.com/ ... search. One of the examples of the implementation of htmlweb.ru/ajax/example/city.php - Visman
  • @Visman, of course, a link to another resource is good. But if the resource is deleted, then this information will not be accessed. You'd rather have the code that is listed in the source as the answer for the common good. - Roman Kozin
  • @RomanKozin, you have already designed everything: R And I hope Google will not delete. - Visman am

1 answer 1

The mysql_* library is already outdated. Use better mysqli or PDO .

Create a script handler as follows:

Php

 $mysqli = new mysqli('127.0.0.1', 'root', '1111', 'db_name'); // Можно без указания БД // $mysqli->select_db($dbname) - Если хотите работать с несколькими БД $look = $mysqli->query("select * from table where `phone` like '{$_POST['phone']}%'"); if ($look->num_rows){ echo json_encode($look->fetch_array()); } 

jQuery

 $.ajax({ type: 'POST', url: 'receiver.php', data: "phone=" + $('#phone').val(), success: function (response) { //Обработка ответа }, $("#phone").rules("add", { required: true minlength: 4 }); }); 

Yes, and an onkeyup event on the phone field. Or handle it immediately with jQuery .

Hope my answer helps you.

And do not forget to protect your code from SQL and XSS injections at a minimum.

  • one
    The code, of course, would be good to bring one that does not allow for SQL injection. Or in the answer to mention it. - Visman
  • @Visman, I hope that everyone who begins to learn PHP should be aware of such dangers as SQL-Inj and XSS as a minimum - Roman Kozin
  • one
    In vain you hope;) - Visman
  • @Visman,: D Itself was once such) But with experience it came) - Roman Kozin
  • @RomanKozin, I support Visman. In the current form, your code cannot be used due to a vulnerability to SQL injection. - Dmitriy Simushev