Reputation: 3301
Can anyone guide me as to how to set a form to be enabled for a certain time period in a day.
Or how to limit the Total Number of Form submissions per day or per week etc.
For e.g.
I need the form to automatically be disabled as soon as 100 form submissions have been sent in a day or an hour etc.
100 submissions should be added to a table per day. The form should not accept any more submissions for the rest of the day, as soon as 100 entries have been sent.
Requirement: We need to handle a certain number of questions/submissions per day. The form should be disabled once the number of submissions in a certain table reaches 100 or any other number.
Which MySQL Query can I use ?
Or can the form be disabled by Javascript ?
Here is a simple form:
<p><label for="name">Name</label><br />
<input type="text" name="name" value="name" id="name" size="37" /></p>
<p><label for="email">Email Address</label><br />
<input type="text" name="email" value="email" id="email" size="37" /></p>
<p><label for="comment">Comment</label><br />
<textarea name="comment" rows="8" cols="35" id="comment">comment</textarea></p>
<p><input type="submit" name="submit" value="Submit" /></p>
Update: Thanks to Quentin
here is the code i have reached.
SELECT count(*) AS count FROM data where channel_id='2'";
if (count <= 100) {show the form} else {show a message that the form is closed}
The table has 3 fields , [year] [month] and [day]. how do I use the CURDATE () in this manner as the date is divided into 3 fields ?
Upvotes: 2
Views: 2671
Reputation: 3301
The following code worked:
SELECT count(*) AS count FROM `channel`
where `day` = DAY(CURDATE())
and `channel_id` = '2'
Now it needs to be tested in realtime, if it works with the CMS or not. Does the form hides or not.
The form does hide if i manually try to edit " {if count <= '100'} " , the form does hide, but i am concerned if it will work automatically. I think ill have to wait till morning.
Upvotes: 0
Reputation: 2657
If I understand this right this is a form displayed on your website (not the control panel publish form?) - if so this fairly straightforward. Do a record count check on your DB (if that's where you're storing your form data) based on a timestamp as others have shown with the example SQL statement. When you're reached your limit you can display the form with no post value and disable it using javascript & style it so it looks un-usable (or don't display it all).
In pseudocode:
Set timeframe = week, day, etc.
Set maxsubmissions = 100 (or whatever)
Set sumbissioncount = Query db to get number of submissions within timeframe
If submissioncount is less than maxsubmissions
{embed form}
Else
{embed disabled form}
End if
Note sql examples given will only count per day, not week, you'd have to modify that for a week for example - to make it generic use the between clause or plunk vars between the ranges in examples above: see related thread: Date Range Query MySQL
Upvotes: 0
Reputation: 1527
Add a time field to your database and instanciate with curdate then do this query
SELECT count(id) from TABLE where DATA=CURDATE();
Then you can implements a function to in your code to handle the form submission.
Upvotes: 0
Reputation: 85
On your page displaying the form: Query your database: Use a MySQL COUNT on an index to get num rows (1row = 1 response??) where submitday = today
see the folloing links, esp the second, on extracting time/date http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
if numrows = 100 use javascript to 'display:none' your form, 'display:block' your div
have <div id="toomuch" style="display:none;"><p>I'm sorry this form is closed for the day, try again tomorrow</p></div>
Upvotes: 0
Reputation: 943163
You can't do this client side.
A query along the lines of:
select count(*) from myTable where myDateColumn >= CURDATE();
should do the trick.
Use it when deciding if you should generate the form, and when deciding if you should accept the form submission.
Upvotes: 2