javascript - Restrict the user to submit a form once in a day using Jquery or J.S or php -


i have form , want 1 users submit once day. if user submitted more 1 time in day message show sorry done!

you when user changes page, data gone. javascript/jquery should not handle:

make hidden input, when submit pressed, have submit function:

<form id=theform> <input type=hidden name=last id=last></input> <input type=button onclick="submitfunction()"></input> </form> 

then have script

<script>  submitfunction = function() {   var one_day = 60 * 60 * 1000 * 24; /* ms */   var lasttime = document.getelementbyid('last').value;   document.getelementbyid('last').value = new date();   if (lasttime == "" || !lasttime)   {     document.getelementbyid('last').value = new date();     document.getelementbyid('theform').submit();   }   else if (((new date()) - lasttime ) < one_day)   {     alert('you must wait 1 day');   }   else {     document.getelementbyid('theform').submit();   }  } </script> 

to solve problem suggest using php database recording when user last submit form. way data kept persistently.

to php: first create database; example call test database , table called lastcheck. in form table have 1 column : last_time set timestamp;

<?php      $last = "";     $servername = "xxx";     $username = "xxx";     $password = "xxx";     $dbname = "test";      try {         $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password);         $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);         $stmt = $conn->prepare("select last_time test.lastcheck order last_time asc limit 1");          $stmt->execute();          // set resulting array associative         $result = $stmt->setfetchmode(pdo::fetch_assoc);          foreach($stmt->fetchall() $k=>$v) {              $last = $v;         }     }     catch(pdoexception $e) {         $conn = null;     }     $conn = null;      echo '<form id=theform>          <input type=hidden value="'.$last.'" name=last id=last></input>          <input type=button onclick="submitfunction()"></input>          </form>'; ?> <script>  submitfunction = function() {   var one_day = 60 * 60 * 1000 * 24; /* ms */   var lasttime = document.getelementbyid('last').value;   document.getelementbyid('last').value = new date();   if (lasttime == "" || !lasttime)   {     document.getelementbyid('last').value = new date();     document.getelementbyid('theform').submit();   }   else if (((new date()) - lasttime ) < one_day)   {     alert('you must wait 1 day');   }   else {     document.getelementbyid('theform').submit();   }  } </script> 

basically thats left method form. should have action leads new php page saves new date database , whatever code want done when form submit, or redirect previous page after saving new date database, etc.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -