jQuery event without user interaction

Asked

Viewed 68 times

0

I am accessing the Mysql database and giving a Select in a table q will return several ddd’s and phones, with this data I would send to a server via get or post using jquery with ajax. The problem is that there will be no user interaction, it will only run on the server. Currently, I am listing with PHP and sending with a click on the button. How to do jQuery and Ajax after listing ddd’s and phones send to another server this data one after the other?

  • 1

    You can show the code you have and explain what data you want to send without interaction?

  • Couldn’t do everything in the page load?

1 answer

0


I was able to solve it using jQuery’s Focus(). I define that the field will have the focus and create an event for when it is in focus, in this event I send the data using Ajax. The $n variable serves to differentiate the id’s, so when it goes to look for the id’s I put an echo in PHP $n, thus having, each item that is found in the bank an index set by $n and each numbered id. In the end, the focus shifts to the other input because this has no event associated, I did it because, in the tests he was repeating endlessly the sending of the last and with this "way" solved. I don’t 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++;
        }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.