-1

I need to, or might not if there is another solution, check to see if a record exists in the database. I need to check this every 5 seconds or so while the user is on the page, when it isnt there i'll redirect them.

How can I check if the record is there every 5 seconds without refreshing the php page, but do it inline?

Any help would help greatly :)

Deforz
  • 1

1 Answers1

0

You can accomplish this with AJAX (Asynchronous JavaScript and XML), but be warned calling a database that much can make your site slow. Obviously it'd take many users to make it slow, but this is a fair warning. You might also want to program a time out function where if the user hasn't done anything in X amount of minutes, don't submit Y into the database.

But here is the basics of what you're trying to accomplish.

<script type="text/javascript">
    function myAwesomeFunction(){
        $.ajax({ url: '/path/to/mysqlcalling' }).done(function(data){
            //Call back data. The data variable is your callback data, though you will have to figure out how you want it to be used.
            setTimeout('myAwesomeFunction()',5000); //5 seconds = 5000 milliseconds
        });
    }
</script>

I'm going to assume you're using jQuery, otherwise this will be a painful step. You could also use jquery's POST functionality, but if you're not submitting data and are just calling the database, this is sufficient.

More information on jQuery's AJAX Function.

More information on jQuery's POST Function.

Jake
  • 1,469
  • 4
  • 19
  • 40