0

I was wondering what a good way to load an external web page (same server) would be. I have tried .load() and .get() however, The external page has a php script that spits out information every few seconds, the .load() and .get() only load it after the php is done. I have tried iFrame with does load it displaying the information being outputted by the PHP script. However, I don't really like to use iFrames. Thanks!

Username
  • 367
  • 1
  • 4
  • 13
  • Can you show the php script and the ajax call you are currently using? It is difficult to tell what is happening without code. – 000 Sep 12 '13 at 22:54
  • @JoeFrambach, I am not using Ajax. I am using an iFrame. The PHP script doesn't really have anything to do with it. It just has a for loop and spits information every time it goes through. I need that information that's why I am using an iFrame, but I'm wondering if I could display that information with .get() or .load() instead of it just loading after the PHP for loop finishes. – Username Sep 12 '13 at 22:58

1 Answers1

1

If your goal is for the PHP information (that is spit out every few seconds) to be updated on your site, then what you want to do is use AJAX, inside a setInterval routine.

See this post for the basics of AJAX -- it really is simpler than you might think. (You might first want to look at the simple examples linked at bottom).

Once you've got a simple ajax exchange happening, put that into a function called, for example, doAjax() -- and then create a setInterval, like this:

setInterval('doAjax();',60000);

Here is an important note when considering setInterval


Following is a simple copy/paste(able) example that will let you see exactly what I mean:

HTML/javascript: index.php

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        
        <style>
            #timeDiv{width:40%;height:200px;background:wheat;padding:10px;}
        </style>
        
        <script type="text/javascript">
            $(document).ready(function() {

                doAjax();
                
                window.setInterval(function(){
                    doAjax();
                },2000);
                
            }); //END document.ready

            function doAjax() {
                $.ajax({
                    type: "POST",
                    url: "your_php_processor.php",
                    success: function(myData) {
                        $('#thetime').html(myData);
                    }
                });
            }

        </script>
    </head>

<body>

    <div id="timeDiv">
        The time is: <span id="thetime"></span>
    </div>

</body>

</html>

Now, the PHP side... your_php_processor.php

<?php

    $d = date("h:i:s");
    echo $d;
halfer
  • 19,824
  • 17
  • 99
  • 186
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • What post for the Ajax? – Username Sep 12 '13 at 23:03
  • My PHP script outputs values and all of those values are needed. However, when I run it again the values start from the beginning, so I can't really do setInterval() – Username Sep 13 '13 at 01:56
  • @SiDiX Then (on the PHP side) store the values in SESSION variables and retrieve them each time the PHP script is run. So, each AJAX call, the PHP script simply carries on... read/update/re-save the SESSION vars... echo the updated results. – cssyphus Sep 13 '13 at 19:03
  • However, there are like 36 different values that are automatically generated. This is unique for every user. – Username Sep 14 '13 at 12:34
  • @SiDiX Probably we (SO helpers) are unable to fully understand the complexities of your task. If you think it worth the time, could you please re-write your question and tell us exactly what you need to do -- and as a group we will put on our thinking caps and come up with a solution for you. – cssyphus Sep 15 '13 at 14:57