Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Thanked 0 Times in 0 Posts

    Advice with XMLHTTPRequest

    I have a complex issue I am having trouble resolving. Basically I am trying to create a live user queue with some combination of ajax and php. When someone enters the queue I insert their ID into the database. Now, here is my problem. Disclaimer: I'm not an expert programmer.

    I have page 1 with a setinterval command that is running an xmlhttprequest to page 2. Page 2 (completely php) is basically just counting the rows in the database to see how many users are in the queue and reporting back a live update of how many users are there. I am trying to figure out how to do a redirect of Page 1 whenever Page 2 has found 5 users in the queue. Is there any way to pass a variable back to page 1 or something? Or do I need to code some javascript accessing the database instead of php?

  2. #2
    Regular Coder
    Join Date
    Feb 2009
    NJ, USA
    Thanked 70 Times in 69 Posts
    Ok, first, JavaScript cannot access a database on your server directly on its own. JavaScript is completely client side, which means that it only runs inside of the client user's browser. Therefore, it can really only control the browser's behavior, and manipulate the web page that the user sees. (Just imagine if the user's browser could access your database directly. That would be quite a security problem for anyone who wanted to exploit that with a few DELETE statements )

    So because of that, all JavaScript really can do is make a request to a file on your server (in your case, a .php file), to do the database access on its behalf. That php file can then return whatever result that you want your JavaScript code to receive. You achieve this in JavaScript with an XMLHttpRequest; more commonly known as an AJAX request.

    To make your php page "return" any piece of data, all it has to do is echo/print it onto the page's output. The AJAX request receives everything that the php page outputs as the "response" from the php page. So if your php page echo's "5", then that "5" can be read by some JavaScript code, and saved into a JavaScript variable. You can then use that variable to write your redirect.

    If you can, post up both the JavaScript and the php code that you have at this time. With that, I can direct you to exactly what you need to do, rather than trying to create an example while making assumptions as to how your code is working at this time.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts