Pass PHP ID for javascript to a page

Asked

Viewed 81 times

0

I would like to send the id I pick up with php and send to a page every 30 seconds...what I’ve done so far:

$idtarefa = $_GET['id'];

get the id, do all the html and ai in javascript I did it:

<script type="text/javascript">
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState === 4) {
            if (ajax.status === 200) {

                // Tudo correu bem NA REQUISIÇÃO!

            } else {
                // debug:
                console.log(ajax);
                alert('Erro na requisição.');
            }
        }
    }

    setInterval(function() {
        ajax.open('get', 'atualiza_base2.php?id=<?php echo $idtarefa ?>');
        ajax.send();
    }, 3000);
</script>

The idea is to update the time every 30 seconds according to the id in a mysql table, I looked at several scripts and went there, but surely there is something wrong that I do not know what is...in the file update_base I wanted to get the id with get dnv

1 answer

2


Here’s what you can do. Before including or writing javascript code, set a constant with your id, example:

<script>
    const idTarefa = <?php echo $idTarefa ?>;
</script>

And your setInterval will look like this:

setInterval(function() {
    ajax.open('get', 'atualiza_base2.php?id='+idTarefa);
    ajax.send();
}, 3000);

To get the id again, just do $id = $_GET['id'];

If you haven’t solved your question, give more details about the file update_base.php

  • Dude, it didn’t work...: T

  • Worse than that is the key to an entire system...and I can’t get past that id, banging pin for 2 days already

  • You can give more information about the error and how it is being implemented?

Browser other questions tagged

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