Hello. Such a task. There are two input fields. Both are on the same page. And in these fields, when you enter text, prompts appear with possible options (from the database).
I also need to do so that in the second field, the hints depend on what is entered in the first field. For example, in the database there are tips for the first field: Machine, Tree. And for the second field, in the base there are tips for both options, for Car: truck, passenger. For Tree: Pine, Birch.
And if the user wrote Machine in the first field, then in the second field there should be hints only for the machine.
How to make a choice from the base, I know. I do not know how to formulate a sql query without reloading the page, and immediately output it.
I tried to do this:
$('#street').blur(function(e) { var name_st = this.value; $.ajax( { type: 'POST', url: '/inc/adres.php', data: {info:name_st}, 'success' : function (result) { } }); }); With this code, I determined what was entered into the first field, and after exiting this field, I send POST to the php file (adres.php), in which there should be a selection from the database, by the entered word.
I display the adres.php file by inserting it at the very beginning of the page.
File ID:
if (isset($_POST['info'])){ $sql_streetj = mysql_fetch_assoc(mysql_query("SELECT * FROM `street` WHERE `name_ru` = '".$_POST['info']."' ")); $sl2 = array(); $all_city_db2 = mysql_query("SELECT * FROM `houses` WHERE `id_street` = '$sql_streetj[id]'"); while ($city_name_ru2 = mysql_fetch_assoc($all_city_db2)) { $sl2[] = $city_name_ru2['name_ru']; } }else{ $sl2 = array(); $all_city_db2 = mysql_query("SELECT * FROM `houses`"); while ($city_name_ru2 = mysql_fetch_assoc($all_city_db2)) { $sl2[] = $city_name_ru2['name_ru']; } } The logic of this file: If there is a POST, then an array with hints should be formulated for the second field. If there is no POST, then an array is formed, and in it all possible prompts, for all variants, without sampling.
The result is that when the page is loaded, an array is formed with all possible prompts (since there is no POST yet), and after filling the first field, nothing happens, the prompting array remains the same, as if POST was not sent.
Maybe I'm missing something? Maybe on the page php code is not updated?
I would be very grateful for the help. PS knowledge js I have not.