jQuery event without user interaction

0

I am accessing the MySQL database and giving a select in a table that will return several ddd's and phones, with this data I'll send to a server via get or post using jquery with ajax. The problem is that it will not have user interaction, it will only run on the server. Currently, I'm listing with PHP and by clicking the button. How would you do for jQuery and Ajax after listing the ddd's and phones send this data one after another to another server?

    
asked by anonymous 02.04.2015 / 14:15

1 answer

0

I was able to solve using jQuery's focus (). I define that the field will have the focus and create an event for when it is in focus, in that event I send the data using Ajax. The $ n variable serves to differentiate the id's, so when it goes looking for the id's I put to PHP to give an echo in $ n, thus having every item that is found in the database an index defined by $ and each numbered id . In the end, the focus changes to the other input because it has no associated event, I did it because, in the tests it was repeating the sending of the last one infinite times and with that "way" solved. I do not need user interaction.

<?php
        $connect = mysqli_connect("localhost", "login", "senha", "banco") or die (mysql_error ());
        $sql = "SELECT 'ddd_deps', 'tel_deps', 'nome_deps' FROM 'deps' WHERE 'sit_deps' = 1 AND 'usu_login_usu' = '".$_GET['usu']."'; ";
        $result = mysqli_query($connect, $sql);
        $n=1;

        //lista os dados em inputs para puxá-los pelo id
        while ($row = mysqli_fetch_assoc($result)){
            echo "<input type='text' name='ddd".$n."' id='ddd".$n."' required='required' value='".$row["ddd_deps"]."'>";
            echo "<input type='text' name='tel".$n."' id='tel".$n."' required='required' value='".$row["tel_deps"]."'>";
            echo "<input type='text' name='nome".$n."' id='nome".$n."' required='required' value='".$row["nome_deps"]."'><br>";
            ?>

            <!--script para enviar para servidor via get-->
            <script>
                $(function(){
                    $("#nome<?php echo $n; ?>").focus();
                });
                $( "#nome<?php echo $n; ?>" ).focus(function() {
                    $.ajax({
                        type      : 'get',
                        url       : 'http://'+ $('#ip').val()+':8443/server',
                        data      : 'ddd='+ $('#ddd<?php echo $n; ?>').val() +'&tel='+ $('#tel<?php echo $n; ?>').val() +'&nome='+ $('#nome<?php echo $n; ?>').val(),
                        dataType  : 'html',
                        success : function(txt){
                            $("#ddd<?php echo $n; ?>").focus();
                        }
                    });
                });
            </script>

            <?php
            $n++;
        }
?>
    
03.04.2015 / 18:25