A time-sensitive form lets you enter and submit the information within the time limit being specified. In this tutorial, we will create a simple form that allows you to enter your name within 5 minutes of form being loaded.

If you failed to do so, it would show an error message that you are too late to submit the form.

PHP Time Sensitive Form

Time Sensitive Form

Create a Practice.php

<!DOCTYPE html>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
*{box-sizing:border-box;}
body{font-family: 'Open Sans', sans-serif; color:#333; font-size:14px; background-color:#dadada; padding:100px;}
.form_box{width:400px; padding:10px; background-color:white;}
input{padding:5px; width:100%; margin:5px 0;}
.warning{color:red; font-size:90%;}
.shadow{
  -webkit-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
-moz-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);}
.pic{text-align:left; width:33%; float:left;}

</style>

<body>
 <div class="form_box shadow">
   <form class="" action="practice_ac.php" method="POST">
     <h4>Time Sensitive Form</h4>
     <p>Enter your name!</p>
     <div class="warning">
       You have only 5 min to enter your name!
     </div>
     <input type="text" name="query" value=""><br/>
     <INPUT TYPE="hidden" NAME="time" VALUE="<?php echo time(); ?>">
     <input type="submit" name="submit" value="Submit">

   </form>
 </div>
</body>
</html>

Create a Practice_ac.php

<?php
echo "Form was loaded at :" . date("H:i:s", $_POST['time']); //28
echo "<br/>";

$time_limit = $_POST['time']+300;
echo "You had to submit form within ". date("H:i:s", $time_limit); //33
echo "<br/>";

$curr_time = time();
echo "You submitted form at ". date("H:i:s", $curr_time); //33
echo "<br/><br/>";
if($time_limit < $curr_time) //33>28
{
  echo "Sorry! You submitted form too late!";
}else{
  echo "Congratulation! The form was submitted within Time!";
}

?>

Submitted Form

The code is relatively simple. It considers three-time units. One when the form loads, second is the time limit (time()+300), and third is the time you submit the form.

Now compare the submission time with the time limit. If you have exceeds the time allowed, it will display an error message.

Related Articles

Last modified: March 14, 2019

Comments

Write a Reply or Comment

Your email address will not be published.