Update the jquery title

Asked

Viewed 237 times

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>
      <script type="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 the F5.

  • PHP runs only once, so it is not possible to do a new count using php this way... what you can do is send a request in ajax to fetch the values in php.

3 answers

1

I always used window.setInterval and it worked right. That 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 only loaded once, javascript is a client language, in which case it could be updated for the client to see. In order for the PHP variable to be updated, you will need to use AJAX.

  • Actually the problem is in the php snippet, php runs only once on the page so it is not possible to get new values this way.

  • Right. It’s just that I bring the bank count. Can PHP be interfering? In this case, how should I make it automatic?

  • @Jose.Marcos posts his php code to take a look

1

I don’t know if it’s around here, because I’m new to the site. I 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>
      <script type="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>
  • I removed the PHP code and put manually and still only updates when I press F5.

  • Oh yes, you could have edited your question and even put it together ;)

  • Okay. I’m sorry about that. I’ve updated my question, but I can’t remove my answer.

1


Don’t mix code 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 so (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>
        <script type="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 fileConsultaMyslq.php do it:

 .....
   $sqlContar = mysqli_query($conexao,"SELECT COUNT(*) AS ContarTeste FROM teste");
   $jmContar = mysqli_fetch_object($sqlContar);
   echo json_encode($jmContar);
 ?>
  • Okay, Sneeps. I’ll try it this way. Thank you!

Browser other questions tagged

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