-2

I was trying to reload a div in a PHP function but it does not work. Everything work but not the div reload:

function refresh() {
    $servername = "localhost";
    $username = "root";
    $password = "";
    $db = "wp_4lous";
    $conn = new mysqli($servername, $username, $password, $db);
    $checklog = mysqli_fetch_row(mysqli_query($conn, 'SELECT numero_voti FROM voti WHERE candidato="LOG"'));
    if ($checklog[0] == '10') {
        mysqli_query($conn, 'UPDATE voti SET numero_voti="0" WHERE candidato="LOG" AND numero_voti="10"');
        echo '<script> updateDiv(); </script>';
    }
};
?>

<script type="text/javascript">
function updateDiv()
{ 
    $("#here").load(window.location.href + " #here" );
}
</script>
  • what errors do you see in the console? – Bravo Sep 19 '21 at 07:13
  • you aren't calling `refresh()` at all in your script and that's not the way that it should be implemented – Flash Thunder Sep 19 '21 at 07:14
  • 4
    You do know that php and js are executed at different times and on different machines, right? – Olaf Kock Sep 19 '21 at 07:15
  • PHP runs on the server, where is JavaScript runs on the browser (client). The server responds to requests made by the client. A server cannot usually communicate something to a client, without a corresponding request made by the client. Please explain what you are trying to do, in order to get better help. – Nice Books Sep 19 '21 at 07:37

1 Answers1

0

You can try a different approach, the way you are doing will not work. Use a php variable and assign something usefull to understand that your SQL update is done, and use that variable inside the script to do the work.

Sample code below - I have used a variable $recUpdated, and modified your code, take a look.

    $recUpdated = "NO";

    function refresh() {
            $servername = "localhost";
            $username = "root";
            $password = "";
            $db = "wp_4lous";
            $conn = new mysqli($servername, $username, $password, $db);
            $checklog = mysqli_fetch_row(mysqli_query($conn, 'SELECT numero_voti FROM voti WHERE candidato="LOG"'));
            if ($checklog[0] == '10') {
                    mysqli_query($conn, 'UPDATE voti SET numero_voti="0" WHERE candidato="LOG" AND numero_voti="10"');
                    $recUpdated = "YES";
            }
    };
    ?>

    <script type="text/javascript">

        <?php if($recUpdated == "YES") { ?>
            $("#here").load(window.location.href + " #here" );
        <?php } ?>

    </script>
Subhashis Pandey
  • 1,433
  • 1
  • 12
  • 15
  • That's not working. I imported that function with a require in another file. When a button is pressed, it calls the function. But it does not refresh the page – Lorenzo Alinovi Sep 19 '21 at 08:06
  • Console log $recUpdated and check its value. As you are using another function on another page use it as a return variable, the example you provided here should work, but you are using it differently – Subhashis Pandey Sep 19 '21 at 08:34