0

I'm trying to get a span to refresh with a php function which queries the database for spaces. However, I can't figure out how to keep it refreshing the actual database content dynamically (preferably via a timer). Here's what I have so far

<script type="text/javascript">
window.onload = startInterval;
function startInterval()
{
        setInterval("startTime();",1000);
}

function startTime()
{
    document.getElementById('spaces').innerHTML = <?php echo get_spaces($id)?>;  
}
</script>
2
  • Setup a script that nicely formats your data (json_spaces.php) and just query it via AJAX every so often. Commented Oct 24, 2012 at 1:06
  • <?php echo get_spaces($id)?> will execute server side and echo a static value. The JavaScript is then compiled client side with that static value so it won't change on subsequent executions (in JavaScript). Commented Oct 24, 2012 at 1:06

3 Answers 3

1

You're going to have to use Ajax to load content dynamically without reloading the page.

You will have to make a PHP script that will output only the content you want to load dynamically on the page and then load this script with Ajax.

There is an easy way to do this with jQuery, see this example: http://www.sitepoint.com/ajax-jquery/

Sign up to request clarification or add additional context in comments.

Comments

1

I assume get_spaces executes a query by a parameter id

Have this function return a result by

 echo json_encode($result);

Now, instead of function start_timer calling your php function statically as your example has above, have it return via ajax

 function start_timer(){
 $.ajax ({
 url: get_spaces.php?id=1, // replace the parameter with dynamic assignment
 dataType: json,
 Success: function(result){
 //iterate thru result json obj
 }
 });

Comments

0

Like the other posters pointed out, you'll have to use AJAX to make this work. Here is some pseudo code that might help you,

<script type="text/javascript">

window.onload = // Start your setInterval, pass it interval.ajax_call()

var interval = (function(

    // Store data here
    var data;

    return {

        // Make your ajax call within this function
        ajax_call: function(),
    }

)();
</script> 

This way you can store the information from your database into data via the ajax callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.