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.

    2 answers 2

    In order to give something to result in js, you need to create an answer and output it in json , html format or in some other way. Next file should not be included but make it a separate page. And modify:

    for example фаил /inc/adres_json.php

     header('Content-type: application/json'); 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(); } echo json_encode($sl2); 

    so you will get the required array in the result variable and you can parse it already on js

     $('#street').blur(function(e) { var name_st = this.value; $.ajax( { type: 'POST', url: '/inc/adres_json.php', data: {info:name_st}, 'success' : function (result) { console.log(result); // тут будет массив пришедший от php } }); }); 

    ps mysql - outdated, use mysqli or pdo

    pps You can also generate json with all the hints and already play with this array in js without requesting the server once again. those. You came up with two approaches in the question, one request to the ajax server, another processing of hints on js.

    • and for what konsol.log cause? -
    • and how can I work with this array? -
    • @Denis open the developer console and console.log there it will output the array for debug to see what is in the array and figure out how to work with it, because we don’t know how you are going to render these tips. The second question: you have to come up with this, try to read it, and if you have any difficulty, then ask here. Otherwise, the answer is very cumbersome. - Naumov
    • So, do I get an array with hints (json_encode ($ sl2);) after filling in the first field? -
    • on ideas yes ..... - Naumov

    You can look towards UI autocomplete + ajax

    Ie after entering the text in the first input - you send an AJAX request with the value "Machine" and receive data from the database that is associated with the value, after receiving the data you already enter into autocomplete (how to? read the documentation) ... and everything after when the user starts writing in the second input, hints for a particular value will already be displayed.
    PS it is imperative to read how to work with autocomplete UI (for example, here )

    • how to do autocomplete i know i did it. The question is how to make a selection from the database after changing the text in the field - iKey
    • That is, the main problem is "but after I fill the first field, nothing happens, the array with the hints remains the same"? - Arsen
    • Yes. according to the idea, after leaving the field, there should be a POST with the entered word, and by that word there should be a selection from the base. I know all this how to do it. but for some reason the filled array, after sampling does not come - iKey
    • Do you include the adres.php file (!?) in the file where the result should be? - Arsen
    • When you request an ajax request, you do not need to enable anything, @Naumov wrote you a common logic. Read how to work with Ajax habrahabr.ru/post/14246 - Arsen