Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2010, 05:09 PM   PM User | #1
enigma12814
New to the CF scene

 
Join Date: Dec 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
enigma12814 is an unknown quantity at this point
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?
enigma12814 is offline   Reply With Quote
Old 04-03-2010, 02:32 AM   PM User | #2
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
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.

-Greg
Gjslick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:21 AM.


Advertisement
Log in to turn off these ads.