 |
ACM UMD - Association for Computing Machinery
|
| View previous topic :: View next topic |
| Author |
Message |
dalawren

Joined: 08 Feb 2007 Posts: 33 Location: Livonia, MI
|
Posted: Tue Feb 19, 2008 10:49 pm Post subject: PHP, AJAX, and updating/querying a MySQL database (oh my?) |
|
|
So I've been trying to create a little thing on my web space that allows a user to fill out a form which inserts into MySQL database, AND have the table represented by that (now updated) database to be updating automatically without refreshing the website. Dandy, huh? I got it to work kind of okay.
Using AJAX, you can get javascript to call another PHP page which does the insert, and another PHP page that does the query. The problem is sort of represented by this stupid graphic I made:
It's a pretty "abstract" graphic. index.php is where the user inserts and views the table. control.js is the script that uses AJAX to call the insert.php page, which takes the user's input to put it into the database. The problem is that the new insert doesn't always show in the refreshed table. I am about 99.8% sure that this is because the insert is being done after the refresh.
I can't, for the life of me, synchronize the "control" with the "insert". Using the built in attributes of the XMLHttpRequest object (http://en.wikipedia.org/wiki/XMLHttpRequest#Properties) doesn't work. readyState of "4" does not explicitly mean "finished", apparently, although that might just be because the SQL database is still processing all its "stuff".
Aside from this, I've tried using semaphores and other things. The only thing that actually works pretty well is pausing the "control" for a little while between the call to "insert.php" and the refresh (where it says wait in the graphic) using a for loop like..
for(int i = 0; i < 10000; i++);
..just to force it to count to 10,000 before continuing, which is a good enough "pause" for me. But, this isn't exactly efficient or good programming practice, and *still* doesn't work all the time.
So.
Anyone here have any other brilliant ideas? Anyone have any experience with this sort of thing? My work-in-progress is at:
 _________________ - Dave |
|
| Back to top |
|
 |
sbriley

Joined: 30 Mar 2006 Posts: 189
|
Posted: Sat Mar 15, 2008 4:26 pm Post subject: Re: PHP, AJAX, and updating/querying a MySQL database (oh my |
|
|
| dalawren wrote: | So I've been trying to create a little thing on my web space that allows a user to fill out a form which inserts into MySQL database, AND have the table represented by that (now updated) database to be updating automatically without refreshing the website. Dandy, huh? I got it to work kind of okay.
Using AJAX, you can get javascript to call another PHP page which does the insert, and another PHP page that does the query. The problem is sort of represented by this stupid graphic I made:
It's a pretty "abstract" graphic. index.php is where the user inserts and views the table. control.js is the script that uses AJAX to call the insert.php page, which takes the user's input to put it into the database. The problem is that the new insert doesn't always show in the refreshed table. I am about 99.8% sure that this is because the insert is being done after the refresh.
I can't, for the life of me, synchronize the "control" with the "insert". Using the built in attributes of the XMLHttpRequest object (http://en.wikipedia.org/wiki/XMLHttpRequest#Properties) doesn't work. readyState of "4" does not explicitly mean "finished", apparently, although that might just be because the SQL database is still processing all its "stuff".
Aside from this, I've tried using semaphores and other things. The only thing that actually works pretty well is pausing the "control" for a little while between the call to "insert.php" and the refresh (where it says wait in the graphic) using a for loop like..
for(int i = 0; i < 10000; i++);
..just to force it to count to 10,000 before continuing, which is a good enough "pause" for me. But, this isn't exactly efficient or good programming practice, and *still* doesn't work all the time.
So.
Anyone here have any other brilliant ideas? Anyone have any experience with this sort of thing? My work-in-progress is at:
 |
Dave. I don't know if you've already figured this out on your own. But, anywhere that you're using the open(...) function in your "selectuser.js" script, you need to change the "true" parameter to "false". According to the wiki article that you provided a URL to, with you setting that one parameter to true (you're setting async to true), the open(...) fuction is called, and the script is NOT waiting for a response, instead it runs that open(...) call as a thread, and while it's being processed, the script is simultaneously continuing to interpret your script, which is why you have timing issues. Just change it to false everywhere, and it looks like it'll run properly. However, if you happen to have a hiccup, you'll need to build in error handling, otherwise I'm not sure what the user would end up getting, but I'm sure it would be undesirable. _________________ "Gentoo Linux + Laptop + No Blue Screen Of Death = You + Jealousy"
"Just because you're not aware of the problem, doesn't mean it doesn't exist."
"Common sense, isn't so common." |
|
| Back to top |
|
 |
dalawren

Joined: 08 Feb 2007 Posts: 33 Location: Livonia, MI
|
Posted: Sat Mar 15, 2008 7:18 pm Post subject: |
|
|
Wow. Well, that was really easy. It looks like it works rather well. I just ordered a book about AJAX from buy.com earlier today to help me with my grand plan of writing a fantastic database management tool that will probably end up sucking.
Thanks, though! _________________ - Dave |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|