-6

I'm looking for a script that will automatically update a page but not reload it. I have one example, these divs are automatic reloading: http://prntscr.com/603k2n

It's not my page of course, here is link: http://suna.e-sim.org/battle.html?id=13317 (you will need to register).

I tried with jQuery but it won't reload PHP, just HTML. I need to reload MySQL and PHP - is it even possible? i see this guys are made it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mc Filip
  • 5
  • 1
  • 4
  • 1
    You answered your own question: It is possible. – putvande Feb 02 '15 at 11:09
  • 2
    By using AJAX. Call a PHP page that does what it needs, and return the content you need.. simple as that. – putvande Feb 02 '15 at 11:11
  • http://stackoverflow.com/questions/333664/how-to-implement-basic-long-polling – pawel Feb 02 '15 at 11:11
  • Okay, php need to make changes on website, example: move header to right, i have php code for this, but i want to make something like animation, but on user request, like, user click on button right, then header moves to right... if you understand... – Mc Filip Feb 02 '15 at 11:13
  • That is not a change PHP can make, more a JavaScript or CSS thing. And it doesn't involve AJAX. – putvande Feb 02 '15 at 11:16
  • 1
    I will try the “Long Polling” thing... – Mc Filip Feb 02 '15 at 11:19
  • As they told you PHP is working on server side, javascript on browser side. If you want to make animation, move something etc you have to use javascript, not php. PHP works only when you request server, and returns some data, after that you can manipulate this data only on browser side – szapio Feb 02 '15 at 11:20
  • szapio okay, but how? I am noob in javascript... – Mc Filip Feb 02 '15 at 11:22
  • You have to call php script via AJAX, and then on success you have to insert retrieved data where you want. Find some tutorials and learn javascript, without that you won't be able to do it dynamicly. Without JS your only solutions is to reload whole page, but do not try to write function that reload page every second because your site wont work ;). For now you can make a button 'refresh' which reload your all site (redirect to the same address) – szapio Feb 02 '15 at 11:25
  • Thanks, i will search for it :) – Mc Filip Feb 02 '15 at 11:25

2 Answers2

1

To refresh a page without reloading, you have to load the contents of the page through ajax. You could do this as follows:

index.php

<html>
<head>
<script type="text/javascript">
$(document).on('ready', function(){
    setInterval(function() {
        $.ajax({
            type: "GET",
            url: "ajax_refresh.php",
            success: function(result) {
                $('body').html($result);
            }
        });
    }, 3000);
});
</script>
</head>
<body>
    <div class="header">
    Header of website here
    </div>
    <div class="content">
    Content here
    </div>
    <div class="footer">
    Footer here
    </div>
</body>
</html>

ajax_refresh.php

    <div class="header">
    Header of website here
    </div>
    <div class="content">
    Content here
    </div>
    <div class="footer">
    Footer here
    </div>

In the above example the 'url' parameter should be a PHP file that only returns the body of the page you want to refresh. For this example to work, you should include jQuery.

Good luck

Edit

To explain this a little more. You will need a second file that looks exactly te same as your index file. Except that in this second file you do not have html, head or body tags. The content of the second file will be loaded into the first file without refreshing. This is the concept of AJAX.

For further reading: - Introduction to AJAX - W3Schools

Peter
  • 8,776
  • 6
  • 62
  • 95
0

You actually can solve this with jQuery. I recommend using setInterval. An example of how you might use it:

$(document).on('ready', function(){
    setInterval(function() {
    // Code that will update your page
 }, 3000);
});

Remember that 3000 is the number of milliseconds before the refresh happens. So this will be called every three seconds

davetw12
  • 1,815
  • 2
  • 20
  • 27
  • Code can be php? and this goes in – Mc Filip Feb 02 '15 at 12:37
  • @McFilip This is a piece of javascript and you should place it between tags. – Peter Feb 02 '15 at 13:03
  • The code that gets executed inside the script tags should be in JavaScript. What you will be doing is using JavaScript to access your .php file that will fetch the data you use to update your page. – davetw12 Feb 02 '15 at 13:23