Update the jquery title

2
.....
 $sqlContar = mysqli_query($conexao,"SELECT COUNT(*) AS ContarTeste FROM teste");
 $jmContar = mysqli_fetch_object($sqlContar);
?>
<html>
    <head>
        <title>Nome do Site</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><scripttype="text/javascript">
        window.setInterval(function() {
            mudarTitulo();
        }, 1000);
        function mudarTitulo() {
            document.title = "("+<?php echo $jmContar->ContarTeste; ?>+") Nome do Site";
        }
       // setTimeout(mudarTitulo(), 1000);
</script>
    </head>
    <body>

    </body>
</html>

But it only works if I press F5.

    
asked by anonymous 24.04.2015 / 17:03

3 answers

1

Do not mix javascrit with PHP this leaves the source code very messy, learn about ajax in jQuery since you are making use of jQuery.

example leave your html like this (or even separate javascript too):

<html>
    <head>
        <title>Nome do Site</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><scripttype="text/javascript"> 
        function recursiva(){
            var request = $.ajax({
                url: "suaConsultaMysql.php",
                method: "POST", 
                dataType: "html"
            });

            request.done(function( msg ) {
                window.setInterval(function() {
                    document.title = msg;
                    recursiva();
                }, 1000);
            });
        }
        recursiva();
        </script>
    </head>
    <body>
    </body>
</html>

In your file yourConsultaMyslq.php do:

 .....
   $sqlContar = mysqli_query($conexao,"SELECT COUNT(*) AS ContarTeste FROM teste");
   $jmContar = mysqli_fetch_object($sqlContar);
   echo json_encode($jmContar);
 ?>
    
24.04.2015 / 19:38
1

I do not know if it's here, because I'm new to the site. I've created an isolated page to test. Follow below:

.....
 $sqlContar = mysqli_query($conexao,"SELECT COUNT(*) AS ContarTeste FROM teste");
 $jmContar = mysqli_fetch_object($sqlContar);
?>
<html>
    <head>
        <title>Nome do Site</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><scripttype="text/javascript">
        window.setInterval(function() {
            mudarTitulo();
        }, 1000);
        function mudarTitulo() {
            document.title = "("+<?php echo $jmContar->ContarTeste; ?>+") Nome do Site";
        }
       // setTimeout(mudarTitulo(), 1000);
</script>
    </head>
    <body>

    </body>
</html>
    
24.04.2015 / 17:19
1

I've always used window.setInterval and it worked fine. This way:

<script type="text/javascript">
        window.setInterval(function() {
            mudarTitulo();
        }, 1000);
        function mudarTitulo() {
            document.title = "("+<?php echo $jmContar->ContarTeste; ?>+") Nome do Site";
        }
       // setTimeout(mudarTitulo(), 1000);
</script>

However you are trying to update a PHP variable. PHP is a server language, and is loaded only once, javascript is a client language, in which case it could be updated for the client to see. In order for the PHP variable value to be updated, you will need to use AJAX.

    
24.04.2015 / 17:11