How to make a form submit once every week..?

nml375 27 Jul, 2010
Hi,
The submissions-limit is unfortunately only effective within each session. Clearing your cookies would allow you to submit multiple times.

What you need, is to store some kind of token in a database table, containing a timestamp and user id, and then check the database upon submission for any prior tokens.

Achieving this does require some knowledge in both PHP and SQL programming.

/Fredrik
nml375 27 Jul, 2010
Hi again,
That would be possible, and I think there were one or two examples on this here on the forum during the last two months or so. The same idea roughly applies though, just checking user-id in the database as opposed to both user-id and week number.

Assuming a database table such as:
CREATE TABLE `jos_myform_tokens`
(
  `uid` INT(11) NOT NULL,
  `week` INT(2) NOT NULL,
  `year` INT(4) NOT NULL,
  PRIMARY KEY (`uid`, `week`, `year`)
) ENGINE=MyISAM;


I'd use something like this in the "serverside validation" code:
<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = sprintf(
  "SELECT COUNT(*) AS submissions
  FROM `jos_myform_tokens`
  WHERE `uid` = %u
  AND `week` = %u
  AND `year` = %u",
  $user->id,
  $date('W'),
  $date('Y')
);
$db->setQuery($query);
$result = $db->loadResult();

if ($result != 0) {
  return "You have already completed this contest this week. Please come back next week.";
}


And finally something like this in the "on submit after email" code:
<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = sprintf(
  "INSERT INTO `jos_myform_tokens`
  (
    `uid`,
    `week`,
    `year`
  ) VALUES
  (
    %s,
    %s,
    %s
  )",
  $user->id,
  $date('W'),
  $date('Y')
);
$db->setQuery($query);
$db->Query();


This would create a record with user-id, week number, and year for every successful form submission, and also check for the presence of any matching record before accepting the submission.

/Fredrik
This topic is locked and no more replies can be posted.