Stop the submit button with PHP

I have a little homework and classwork page, sometimes also for little exams.

A javascript disables the “Submit your answers” button at the set time.

I’m told, javascript is easy to get by.

Can you suggest a good way to do this with PHP?

I can limit the opening times with this:

if($nowSeconds < $startSeconds or  $nowSeconds > $stopSeconds) {
		//echo 'You are too early or too late!';
		include 'jsclock.html.php';
		//header('location: jsclock.html.php');
		exit();
    } 

But if the page is already open on a student’s computer??

I could catch the time on submission and go to jsclock.html.php if the submission is too late, instead of processing the submission.

What is an elegant way to do this??

You can not!

First, since this is for a class, you must understand what the three parts are. The server-side, client-side and Javascript are. Here are the details.

PHP is SERVER-SIDE only! It only runs on a server, never inside a browser. Therefore, it can not alter anything that is already loaded into the browser. That is why PHP is secure and usually used to load data or client-side display info into a browser. Once posted to the browser, the system becomes CLIENT-SIDE only. Meaning that it runs on your local computer. PHP never gets there. You will never see any PHP code in the source of a browser page, well, unless it is displayed as text to explain code like on this page you are reading. Javascript or JQuery are CLIENT-SIDE programming code systems that run inside various browsers. They can alter things on the live page in a programmed manner.

Now, with that explained, you could create a button which is an HTML input tag and have it go to a Javascript routine. To do that, when you create the button, make it point to a javascript routine. In that routine, check if the button is outside the timeframe. Similar to how you did it in your example. But, you will need to alter the code to keep the time the page was posted or it will not work.

You could use AJAX to call a PHP file and have it checked a saved time frame, but, that is complicated. You can just add a PHP line in the system to set the time and then compare against it. Or you can use timeout() functions to disable things as needed. You can set your button to call a JS routine. Then set a timeout function for like 3000 milliseconds. After the 3000 milliseconds, a variable can be changed and depending on that results, either allow the button or just throw out an alert saying too late.

Not sure if that makes sense to you. Give it a try and post what you come up with… Good luck.

Thanks!

I already have a simple but very effective javascript to disable the Submit button.

I’m always getting “Peter, I can’t send my homework” from students. (“Yes, I know, you must send before 8pm”)

I can block submission of late submissions easily with PHP.

I was just thinking, if it is not just homework, but a real exam, how to force submission before the time limit?

Some kind of automatic send on T minus 10 seconds??

You simply check the submission date versus the time the user pressed the submit button. Either by using PHP or PHP, AJAX and JavaScript. The only difference is the HTML page will reload if just using PHP or it will seam seamless if using the later. However, there still is a handshake so to speak between the client side and the serve side. You can have a message pop saying something “Sorry, but submissions are not allowed after 8 pm!” or something like that. To force submission one would have to use PHP, Ajax and Javascript, plus checking to see if the time limit is going to be succeeded. Personally, I would just give the person a warning message and if they ignore it then they are just SOL.

Wouldn’t forcing a submission before the time limit was exceeded being breaking your own requirement?

It is quite easy to create a count-down timer. You would just have to set it to count down to the 8 PM cut off time, display it and once zero’d out, not let any further buttons to work. Here is some example code on how to do this. Might work out for you.

<!--HTML -  Display the countdown timer as an element -->
<p id="countdown"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Mar 8, 2021 20:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("countdown").innerHTML = "TIME LIMIT EXPIRED - No inputs allowed!";
  }
}, 1000);
</script>

PHP has nothing to do with what you want to do here.

In your case…
PHP is ONLY for output… after that… (its done).
&
Submission (where you can check on criteria/logic via server side)

If you want to manipulate or change (or even automatic/timed things)… this is ALL FRONT END.

As noted by @ErnieAlex

Its easy enough to do a timer, and then at certain ‘time’ (which can also be a difficult thing to deal with, spoofing, time zones…etc)… you can either give you user an alert/message… or simply submit the form via AJAX… and collect the current info they have in the form.

@All: thanks for the tips! I struggle with PHP, I don’t think I will start with Ajax!

@ErnieAlex

Thanks for the timer! Will be useful

Sponsor our Newsletter | Privacy Policy | Terms of Service