0

I have created a button called "Generate" , when click event is performed it should start a timer after reaching 60 sec it should perform an if statement. I tried the following code but its not working.

if(isset($_POST['generate']))
      {
       $timer = time();
        if($timer = $timer+5)
       {
         $query = mysql_query("UPDATE user_login SET password='' WHERE username = 'ajai sandy'") ;
                     $qry_run = mysql_query($query);
       }

In the above code I have used the system time , I could use both the system time or a timer to complete this action.

Ajai Sandy
  • 107
  • 1
  • 2
  • 11

4 Answers4

0

You shouldn't do this on server side! But you could do it with PHP's sleep() method.

stetro
  • 547
  • 2
  • 6
  • 11
  • Working with jQuery would be: $("button").click(function(){ window.setTimeOut(function(){ // do what ever you want },5000) }); – stetro Mar 13 '14 at 14:45
0

Do you want, that 'if' performs after 60 seconds? Then you need to use sleep()

if(isset($_POST['generate']))
  {
   sleep(60);
   if(condition)
   {
    // todo
   }
Patrik Krehák
  • 2,595
  • 8
  • 32
  • 62
  • I dont wanna use sleep() function , I should set a session or a timer after the 60sec period ends it should delete the password varchar in DB . I have rephrased my question pls refer for more info – Ajai Sandy Mar 13 '14 at 15:06
  • So you will need to use javascript, because PHP cannot work like that. After 60 seconds, javasript can start PHP file (something like CRON). I recommend to use jQuery. – Patrik Krehák Mar 13 '14 at 15:15
  • Am sorry I dont have knowledge about jQuery can you point out me to the code resemblence to my query ?? – Ajai Sandy Mar 13 '14 at 15:19
  • You mean, when $_POST is performed, then after 60 seconds is query performed, but without loading or waiting on page? – Patrik Krehák Mar 13 '14 at 15:39
  • But it is not possible in PHP. Or, you can, but you will need to check timer after every command, because in code, you don't know, when 60 seconds passed. Other way is that sleep(), but it will stop your script for seconds. – Patrik Krehák Mar 13 '14 at 18:16
  • Is there any other language that I can use to perform this operation ?? – Ajai Sandy Mar 13 '14 at 18:24
  • As I said, you will need javascript, it will run on background and can controll php. Meanwhile you can set loading info or etc. Then after 60 seconds javascript call other php script to remove password. – Patrik Krehák Mar 13 '14 at 19:24
0

You need to just first check the current time then after you need to use sleep

if(isset($_POST['generate']))
      {
       $timer = time();
     sleep(5)

        //your condition

or you can just use this with client side scripting lanugage as well there is a function setTimeout

window.setTimeout(FUNCTION_NAME,60);
Ravi Sharma
  • 578
  • 4
  • 13
0
if(isset($_POST['generate']))
      {
      sleep(60);
      // AND NOW action;
      }

But rather use JS...

  • 1
    Sorry , let me rephrase my question its like a session once the button is clicked it should wait for 60sec after that it should delete the varchar present in the DB – Ajai Sandy Mar 13 '14 at 14:58