![]() |
saving to database after few seconds
dear all, im newbie here :p
I have this code, after user make a selection, he/she see the data that he/she selected, then, that data name will be automatically save into their record database. is it possible to make that, the record will be saved after user see for few seconds (ex: 5 seconds). so that it wont save immediately ( for consideration that, if user see the data for more than 5 seconds, that means he/she is interested, then it should be saved automatically) here is the code: Code:
<?php |
The way I would do this, and it may not be the best... (no expert here) would be to use a javascript timer which grabs the data after X seconds and uses AJAX to send the data to a php script which records it to the database.
I apologize, but I cannot provide you with an example at this time. |
i see, thanks for your suggestion :-) , do you have any example for this (later is ok..im still trying to find other ways without using AJAX)? im kinda new in PHP, to move on to AJAX, i think it will be hard for me :(
|
The fact is that PHP is a Server-side script language which means that nothing is going to happen or execute without some Client-side intervention.
Unless you trigger the call yourself through GET or POST and such. And in this case would JavaScript be great to handle that delayed saving. Well you could use cronjobs to execute PHP on the server. But that wouldent help the user. |
i see..because i tried the script above, and it can save the "data name" automatically , thats why im thinking, it should be better if it can save after few seconds ( because if user really interested at looking the data name, he/she should spend some times looking at it )..so, javascript is another option to handle this case right? thanks for your suggestion :), im really appreciated it :)
|
Yes it may have been saved "automaticly" but not without the user loading the page.
Cause thats what the user would have to do if your not going with JavaScript. Cause PHP in itself canīt magicly induce a page load and run the code for saving the data. You would need some form of user intervention. Thats why you would need to use JavaScript to make the "Call" to the server using AJAX and calling your script. In other words the user must do something to make the page load. Like click a link or send a form. At least if your not going to use JavaScript. Thats the bottom line. Because as said before. PHP is Server-Side. and JavaScript is Client-Side. good luck :) |
The PHP-function "sleep()" should do it. Here's just a (somewhat nonsensical) sample script that delays a defined action while displaying a countdown, without the use of javascript. You should get the idea ...
Code:
function delayAction($action, $delay=5, $frequency=1, $confirm=FALSE) |
I have put together a very simple example using the method I described. Please see Timer Test Page .. After 5 seconds it updates and says it was updated. If you view source you will see all of the javascript (AJAX) require to do it. You should be able to pick it apart to make it do what you want.
Please note that I am just using basic knowledge gathered from resources. There is proably a better way to handle it, but eh I said I could do it and here is how :P timer.php Page Code: HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">PHP Code:
|
thx @chrisb and @sidisinsane :D , you guys really helps me alot!! i will try to use and report the result :D , goshh..im newbie in this field T_T
|
Dont use browser detection, instead use object detection:
javascript Code:
For more information: Javascript - Object detection |
Thank you, I will add that to my library. :)
|
Why not use Prototype JS for example.
They have a nifty function in there that would be great in this case. Take a look. |
thanks for all of you who shared your idea here, im still trying to combine above function, but still couldnt get result as i expected before,i think that i got some mistakes in my code..T_T i think java script is one of good option my case :) i will put updates in my thread, and if succeed, i will put the results for others who might have same problems :p
|
and don't forget to secure your scripts ;)
|
| All times are GMT. The time now is 03:36 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0