Autocomplete field with jQuery, Ajax and PHP

0

Greetings, I have a DAO.php where I declare the functions that make the requests to the BD, this function in particular:

function select_all_unis(){
        $tecla_pulsada = $_POST['service'];
        $sql = "SELECT * FROM dumies WHERE studies LIKE '".$tecla_pulsada."%'";

        $conexion = connect::con();
        $res = mysqli_query($conexion, $sql);
        connect::close($conexion);
        return $res;
    }

And then in a controller (controller.php) with different 'case', specifically the one I use is:

case 'list_studies':
        try{
            $daolawyer = new DAOShop();
            $rdo = $daolawyer->select_all_unis();
        }catch (Exception $e){
            echo json_encode("error");
            exit;
        }
        if(!$rdo){
            echo json_encode("error");
            exit;
        }else{
            while($lawyer = mysql_fetch_array($rdo)){
                echo '<div>
                        <a class="suggest-element" data="'.$lawyer['studies'].'" id="service'.$lawyer['dni'].'">'.utf8_encode($lawyer['studies']).'</a>
                    </div>';                   
            }
            exit;
        }
        break;

What is called in a js:

$(document).ready(function() {    
//Al escribr dentro del input con id="service"
$('#service').keypress(function(){
    //Obtenemos el value del input
    var service = $(this).val();        
    var dataString = 'service='+service;

    //Le pasamos el valor del input al ajax
    $.ajax({
        type: "POST",
        url: "module/shop/controller/controller_shop.php?op=list_studies",
        data: dataString,
        success: function(data) {
            //Escribimos las sugerencias que nos manda la consulta
            $('#suggestions').fadeIn(1000).html(data);
            //Al hacer click en algua de las sugerencias
            $('.suggest-element').on('click', function(){
                //Obtenemos la id unica de la sugerencia pulsada
                var id = $(this).attr('id');
                //Editamos el valor del input con data de la sugerencia pulsada
                $('#service').val($('#'+id).attr('data'));
                //Hacemos desaparecer el resto de sugerencias
                $('#suggestions').fadeOut(1000);
            });              
        }
    });
});              

});

And this is the html code.

<form>
    <input type="text" size="50" id="service" name="service" />
        <div id="suggestions"></div>
</form>

I just need to know how depending on the keys that the BD press returns me some values or others, for example the BD I have a series of users, with the universities where they studied, and I want that by pressing the key c the BD give me back the universities that start with c. I understand that it would be something like $ _POST ['services]; but I do not know where to put it exactly, the code that I have based on is: link But of course my model structure, controller view is different.

    
asked by Alepla 17.01.2018 в 10:40
source

1 answer

0

The variable service is being sent by AJAX to the module / shop / controller / controller_shop.php? op = list_studies file using the data attribute of the ajax () function:

$.ajax({
...
data: dataString,
...
})

The dataString variable contains the key pressed and you are sending it by the POST method with the name of 'service':

var dataString = 'service='+service;

Therefore it must be accessible in the file module / shop / controller / controller_shop.php? op = list_studies using $_POST['service]

Once this variable arrives by POST to the PHP file you want, all you have to do is form an SQL query with the universities that begin with that letter, something like:

$tecla_pulsada = $_POST['service'];
$consulta = "SELECT * FROM universidades WHERE nombe LIKE '".$tecla_pulsada."%'";
    
answered by 17.01.2018 / 11:07
source