Autocomplete input with two conditions

0

I have the code to autocomplete several text boxes from a selection in a drop-down list.

When I enter a character in the "nickname" imput, a query is activated in the clients table and a dropdown appears with the options that match. From the selection, several inputs are autocompleted from the row information of the selected table.

Then I have another imput "nameconcli_1" in which I have the same script only that it looks in the table "contacts"

I would need to alter the code so that the query "tableclients" takes into account the value selected in the "nickname" imput (the idcli is the column that relates both tables) so that in the drop-down list it only shows the options of contacts for that client.

I understand that I should only add an "AND" to the "WEHRE" in the query I do in the getDetailscontacts.php file, but I have not been able to write it to work.

Thank you very much for your help.

index.php (here is the form and the part of the scrip in java, I have simplified the form to make it clearer)

                               <div class="table-responsive">
                                    <table class="table table-bordered" id="dynamic_fieldcli">

                                        <tr>
                                        <td colspan="3" align="center" style="color:#FFFFFF" bgcolor="#3977B1">DATOS DEL CLIENTE</td>
                                        <tr>
                                            <td>
                                            <input name="idcli" type="hidden" id ="idcli">
                                            <div   class="col-xs-2">
                                                <label for="ex1">ApodoCli</label>
                                                <input class="form-control" name="apodocliente" placeholder="Enter your Name" id='apodocliente' type="text" required>
                                            </div>

                                            </td>
                                        </tr>

                                    </table>
                            </div>

                            <div class="table-responsive" id="dynamic_fieldconcli">
                                <table class="table table-bordered">


                                    <td>
                                    <div class="col-xs-3">
                                        <label for="ex1">Contacto</label>
                                        <input class="form-control" name="nombreconcli[]" id="nombreconcli_1" type="text" required>
                                    </div>

                                    </td>

                                </table>
                            </div>

                               <script type="text/javascript">
                               $(document).ready(function(){

                                    $(document).on('keydown', '#apodocliente', function() {

                                        var id = this.id;



                                        $( '#'+id ).autocomplete({
                                            minLength: 0, // <-- AQUI le indicamos que se despliegue sin esperar a que ingresen datos
                                            source: function( request, response ) {
                                                $.ajax({
                                                    url: "getDetails.php",
                                                    type: 'post',
                                                    dataType: "json",
                                                    data: {
                                                        search: request.term,request:1
                                                    },
                                                    success: function( data ) {
                                                        response( data );
                                                    }
                                                });
                                            },
                                            select: function (event, ui) {
                                                $(this).val(ui.item.label); // display the selected text
                                                var userid = ui.item.value; // selected id to input

                                                // AJAX
                                                $.ajax({
                                                    url: 'getDetails.php',
                                                    type: 'post',
                                                    data: {userid:userid,request:2},
                                                    dataType: 'json',
                                                    success:function(response){

                                                        var len = response.length;

                                                        if(len > 0){

                                                            var linkcli = "editcliente.php?idcli="+response[0]['idcli'];
                                                            var comentcli = response[0]['comentcli'];
                                                            var nombrecli = response[0]['nombrecli'];
                                                            var idcli  = response[0]['idcli'];


                                                            document.getElementById("linkcli_1").setAttribute("href",linkcli);
                                                            document.getElementById('comentcli').value = comentcli;
                                                            document.getElementById('cliente').value = nombrecli;
                                                            document.getElementById('idcli').value = idcli;




                                                        }

                                                    }
                                                });

                                                return false;
                                            }
                                        });
                                    });


                                });



                        ////////////////////////////////////////////////////////inicio conexion db constactos 1//////////////////////////////////////////////////////////////////////////////////
                               $(document).ready(function(){

                                    $(document).on('keydown', '#nombreconcli_1', function() {

                                        var id = this.id;



                                        $( '#'+id ).autocomplete({
                                            minLength: 0, // <-- AQUI le indicamos que se despliegue sin esperar a que ingresen datos
                                            source: function( request, response ) {
                                                $.ajax({
                                                    url: "getDetailscontactos.php",
                                                    type: 'post',
                                                    dataType: "json",
                                                    data: {
                                                        search: request.term,request:1
                                                    },
                                                    success: function( data ) {
                                                        response( data );
                                                    }
                                                });
                                            },
                                            select: function (event, ui) {
                                                $(this).val(ui.item.label); // display the selected text
                                                var userid = ui.item.value; // selected id to input

                                                // AJAX
                                                $.ajax({
                                                    url: 'getDetailscontactos.php',
                                                    type: 'post',
                                                    data: {userid:userid,request:2},
                                                    dataType: 'json',
                                                    success:function(response){

                                                        var len = response.length;

                                                        if(len > 0){

                                                            var idcon = "editconcliente.php?idcon="+response[0]['idcon'];
                                                            var telefonocon = response[0]['telefonocon'];
                                                            var mailcon = response[0]['mailcon'];

                                                            document.getElementById('telefonoconcli_1').value = telefonocon;
                                                            document.getElementById('mailconcli_1').value = mailcon;
                                                            document.getElementById("linkconcli_1").setAttribute("href",idcon);

                                                        }

                                                    }
                                                });

                                                return false;
                                            }
                                        });
                                    });


                                });

                        /////////////////////////////////////////////////////////////fin conexion db constactos1////////////////////////////////////////////////////////////

getdetails.php

<?php
            include "config.php";

            $request = $_POST['request'];   // request

            // Get username list
            if($request == 1){
                $search = $_POST['search'];

                $query = "SELECT * FROM clientes WHERE apodocli like'%".$search."%'";
                $result = mysqli_query($con,$query);

                while($row = mysqli_fetch_array($result) ){
                    $response[] = array("value"=>$row['idcli'],"label"=>$row['apodocli']);
                }

                // encoding array to json format
                echo json_encode($response);
                exit;
            }

            // Get details
            if($request == 2){
                $userid = $_POST['userid'];
                $sql = "SELECT * FROM clientes WHERE idcli=".$userid;

                $result = mysqli_query($con,$sql);

                $users_arr = array();

                while( $row = mysqli_fetch_array($result) ){
                    $userid = $row['idcli'];
                    $apodocli = $row['apodocli'];
                    $nombrecli = $row['nombrecli'];
                    $telefonocli = $row['telefonocli'];
                    $comentcli = $row['comentcli'];


                    $users_arr[] = array("idcli" => $userid, "apodocli" => $apodocli, "nombrecli" => $nombrecli, "telefonocli" => $telefonocli, "comentcli" => $comentcli);
                }

                // encoding array to json format
                echo json_encode($users_arr);
                exit;
            }

getDetailscontacts.php

<?php
        include "config.php";

        $request = $_POST['request'];   // request

        // Get username list
        if($request == 1){
            $search = $_POST['search'];
            $search2 = $_POST['search'];


            $query = "SELECT * FROM contactos WHERE nombrecon like'%".$search."%'";
            $result = mysqli_query($con,$query);

            while($row = mysqli_fetch_array($result) ){
                $response[] = array("value"=>$row['idcon'],"label"=>$row['nombrecon']);
            }

            // encoding array to json format
            echo json_encode($response);
            exit;
        }

        // Get details
        if($request == 2){
            $userid = $_POST['userid'];
            $sql = "SELECT * FROM contactos WHERE idcon=".$userid;

            $result = mysqli_query($con,$sql);

            $users_arr = array();

            while( $row = mysqli_fetch_array($result) ){
                $userid = $row['idcon'];
                $nombrecon = $row['nombrecon'];
                $telefonocon = $row['telefonocon'];
                $mailcon = $row['mailcon'];


                $users_arr[] = array("idcon" => $userid, "nombrecon" => $nombrecon, "telefonocon" => $telefonocon, "mailcon" => $mailcon);
            }

            // encoding array to json format
            echo json_encode($users_arr);
            exit;
        }
    
asked by Xavier Villafaina 22.05.2018 в 20:28
source

0 answers