Jquery Autocomplete with Ajax via PHP and Mysql

0

I have an input that is populated via Jquery autocomplete. Simple and efficient. But I wanted to make that when selecting a number of this input (it's an IMEI cell number) he would already look for (I imagine it is in Ajax) the device to which it is attached (I have a MySql database for this). >

Autocomplete:

TemplatethatshouldappearafterselectingtheIMEIinthepreviousinput:

Theautocompleteisquiet.ButIdonotknowhowIgettheinformationfromtheIMEIModel...InfactPHPandMysqlhavenodifficulty,Ijustdonotknowhowtodothis(JS,Ajax,etc).

I'llpostpartofthecodeheretofacilitate:

<h2>Comeceescolhendoumadaslinhasdisponíveis</h2><divid="form-step-0" role="form" data-toggle="validator">
                            <div class="form-group">
                                <!-- Include jQuery -->
                                 <script src="https://code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
                            <script>
                                  $( function() {
                                     var availableTags = [
                                     <?php
                                     $cont = 1;
                                        for ($x = 47; $x <= 49; $x++) {
                                            //Seleciona as linhas em estoque
                                            $result1 = mysqli_query($conn, "SELECT * FROM sdc_tm_linhas WHERE ddd = '$x' AND usuario LIKE '%ESTOQUE%' ORDER BY ddd ASC");
                                            $r1 = mysqli_affected_rows($conn);
                                            if ($r1 == 0) continue;
                                            while ($row = mysqli_fetch_assoc($result1)) {
                                                echo $row['ddd'];
                                                $linha = $row['linha'];
                                                $juntos = $ddd.$linha;
                                                echo ' "'.$juntos.'", ';
                                            }
                                        }
                                        echo ' "" ';
                                     ?>
                                     ];
                                     $( "#linha" ).autocomplete({
                                        source: availableTags
                                     });
                                  } );
                                  </script>
                                <div class="ui-widget">
                                    <label for="linha">Digite a linha com DDD:</label>
                                        <input id="linha" type="search">
                                </div>

                                 <div class="help-block with-errors"></div>
                            </div>
                        </div>
               </div>

               <div id="step-2">
                    <label class="control-label"><h2>Dados do aparelho</h2></label>
                    <div id="form-step-1" role="form" data-toggle="validator">
                        <div class="form-group">
                           <label for="imei">IMEI:</label>
                                    <script>
                                    $( function() {
                                    var availableTags2 = [
                                    <?php
                                    $cont = 1;
                                    $result1 = mysqli_query($conn, "SELECT * FROM sdc_tm_linhas WHERE linha = 0 AND usuario LIKE '%ESTOQUE%'");
                                    while ($row = mysqli_fetch_assoc($result1)) {
                                        $imei = $row['imei'];
                                        echo ' "'.$imei.'", ';
                                    }
                                    echo ' "" ';
                                    ?>
                                    ];
                                        $( "#imei" ).autocomplete({
                                            source: availableTags2
                                        });
                                    });
                                    $(document).ready(function(){
                                         $("imei").keyup(function(){
                                              $.get("inserir_ajax.php", function(data, status){
                                                    alert("Data: " + data + "\nStatus: " + status);
                                              });
                                         });
                                    });
                                  </script>
                            <input style="min-width: 300px; max-width: 600px;" type="number" min="0" class="form-control" name="imei" id="imei" placeholder="IMEI do aparelho" required>
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
    
asked by anonymous 13.12.2018 / 17:43

2 answers

0

I managed to resolve. After the autocomplete script, I put the following:

$(document).ready(function () {
    $( "#imei" ).focus();
    $('#imei').on('change', function (e) {
        var imei = $('#imei').val();
        //$('#modelo').val($('#imei').val());
        $.ajax({
            type: "GET",
            url: "inserir_ajax.php?imei="+imei,
            data: imei,
            cache: false,
            success: function(data){
                $("#modelo").val(data);
            }
        });
    });
});

It may even be wrong or missing something, but it solves my problem! Thank you to those who were willing to give birth!

    
17.12.2018 / 21:24
0

My failure, I forgot to tell you that I'm using SmartWizard in 5 steps:

    
13.12.2018 / 18:24