khan360
khan360

Reputation: 3

Automatically Enable Submit button at particular time from server using PHP JS

What I'm looking for is this. I have a simple PHP page with a submit button that is disabled by default. I want the submit button to be enabled at a particular time for example 02:00:00

With PHP i can get the time from server with date("h:i:s") which i am using in a JS variable. like var time = "<? php echo date("h:i:s") ; ?>" ;

Now by using setInterval() method every millisec i am trying to compare the value of "time" variable with the particular time i want the button to be enabled. like if(time=="02:00:00") { button.disabled=false; }

But the problem is "time" variable should also dynamically change to meet the conditions otherwise nothing will happen.I can't get any simple solution.Do i require AJAX for this?

Any help will be appreciated :) Thanx!

Upvotes: 0

Views: 1003

Answers (3)

Vincent Ranee
Vincent Ranee

Reputation: 1

Try the following

/* ########## HTML file ######### */
<html>
<head>
</head>
<body onload="timer_function();">
<button type="submit" id="validateIt" style="none">Submit</button>
</body>
<script type="text/javascript">
function AjaxFunction() 
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                    {
                 httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
            catch (e)
                    {
                try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
             }
                catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
            }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {
//document.getElementById("msg").innerHTML=httpxml.responseText;
//document.getElementById("msg").style.background='#f1f1f1';
      }
     if(httpxml.responseText == "02:00:00")
      {
        document.getElementById('validateIt').style.display = 'block';
      }
     if(httpxml.responseText >= "00:00:00" && httpxml.responseText < "02:00:00"
      || httpxml.responseText >= "02:05:00" && httpxml.responseText < "02:00:00")
      {
        document.getElementById('validateIt').style.display = 'none';
      }

    }
    var url="server-clock-ckk.php";
url=url+"?sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
tt=timer_function();
  } 
function timer_function(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('AjaxFunction();',refresh)
}  // Display server time ends
</script>
</html>
/* ########## PHP file server-clock-ckk.php ######### */ 
<?Php
echo date("H:i:s", time());
?>

Upvotes: 0

Noble Mushtak
Noble Mushtak

Reputation: 1784

I would update the time variable using JavaScript:

var curTime = new Date();
var firstColon = curTime.toString().indexOf(":");
time = curTime.toString().substring(firstColon-2, firstColon+6)

I'm a Python person, but I think PHP servers work similarly in that when going to a link, a GET request is sent, and then data is sent back, possibly a dynamically created webpage with Python code, but that webpage can't contain any Python or PHP. Those languages are strictly server-side. You could send a request to the server to get the time, but that would be really inefficient and it's better to do this client side then constantly dynamically change the webpage with requests to your PHP server.

Since this is about disabling a button, if the button sends a request to the server, remember to check that the time is right server-side just in case someone tampered with the webpage using their JavaScript console client-side.

Upvotes: 1

sathishpaul
sathishpaul

Reputation: 122

Isn't this enable/disable done better at server side? Why do you need to enable the submit button in JavaScript at all? Will it be too bad of a solution to simply inform the user that they cannot submit operation is not available at the moment? They can always refresh the page or come back later to see if it is enabled.

Alternatively, you can simply have an ajax request that periodically pings the server to see if the button can be enabled or not. But know that a savvy user can use Web Inspector tools available in modern browsers to change DOM state. Always validate your operations at the server side. Hope this helps.

Upvotes: 0

Related Questions