Jump to content

Trigger an action on new insertion


Zilee

Recommended Posts

Hi. How would I be able to either call a piece of ajax or php code when a new insertion has been done on a peculiar table?Basically I require the need to see in real-time on a web page something that might have been inserted by another user over the web(At the moment it's been insert I should see the result updated). I just need to know how this could be done(Suggestions?) no need for code examples.I thought of a cheap method by basically checking the number of rows in a table and depending on x many rows then update the page with the last x-length insertions. My only issue with this solution is that having this function execute every second would probably not be very optimal.Thanks.

Link to comment
Share on other sites

Which exact database?
MySQL(Latest version) managed by phpmyadminHmm now that I think about it I don't know if it would be wise to echo a javascript piece of code as a string when an insert has occured that would afterwards be evaluated using eval() when the ajax gets the answer from the server-side page which could simply call another ajax function to update the page.
Link to comment
Share on other sites

You could always use a Flash file that loops every 10 seconds or so and pulls it's data from a .php file that is counting the number of rows. I am using something similar for a Point of Sale Dashboard that calculates Net Sales, Customer Count, etc. in real time. Let me know and I can throw together a quick example.

Link to comment
Share on other sites

You could always use a Flash file that loops every 10 seconds or so and pulls it's data from a .php file that is counting the number of rows. I am using something similar for a Point of Sale Dashboard that calculates Net Sales, Customer Count, etc. in real time. Let me know and I can throw together a quick example.
Hmm, I try to avoid using flash. Unless it's minimal I could not opt for that.
Link to comment
Share on other sites

Wouldn't the simplest approach be to just increment an integer (global var) whenever this update occurs? You could also write a SQL trigger to update a small table. The other code could then query this table repeatedly.

Link to comment
Share on other sites

Wouldn't the simplest approach be to just increment an integer (global var) whenever this update occurs? You could also write a SQL trigger to update a small table. The other code could then query this table repeatedly.
Yes, but wouldn't then I have to verify if the integer has changed? I would also still have to "query this table repeatedly" as in every second to notice a change. I just thought the logical approach would be an intervention as soon as an update occured instead of looking up a change every second. That's just me though.The more I think about it and the more I believe I will have to implement a timer to verify on a set interval if any change has occurred since the remote user will probably not interact with the page to trigger anything.
Link to comment
Share on other sites

You're asking for a server "push", as opposed to the client "pull"-ing the information... that's not possible with current JavaScript APIs.Web Sockets attempt to add this kind of possibility, but they're currently only implemented in some browsers with varying and patchy support.There's also the other possibility... you can perform a single HTTP request to the server, and have the server block (not output anything) until there's a change. The server should actually also output a byte every few seconds to keep the client connected and extend PHP's timeout so that it can remain connected itself. Still, the basic idea remains.The problem with that approach is that you're limiting the possible simultaneous connections to your server because the "channel" remains opened. Normally, in the 2-3 seconds between HTTP request, another batch of HTTP requests could be served. Web Sockets actually attempt to fix this by having the client listen to requests from the server, which means the server could in the meantime serve others (i.e. the best of both current approaches).

Link to comment
Share on other sites

You're asking for a server "push", as opposed to the client "pull"-ing the information... that's not possible with current JavaScript APIs.
That's rather fascinating. I will have to look into it. Thanks for bringing me closer to my goal.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...