Help - Search - Members - Calendar
Full Version: Automatically Disable
HTMLHelp Forums > Programming > Server-side Scripting
SarahKay
Hello everyone..

I am currently on contract to be a webmaster for a company, and an individual asked me the other day if it would be possible to automatically 'end' (shut down, not allow any other submissions etc) an html form after a certain amount of submissions.

I've been trying to figure out a way to do this.. and thought maybe someone on here would be able to brainstorm with me smile.gif

The form is currently using php to add the information to a data base and is in the form of;

if(!isset) {
// Display form if submit button has not been pushed}

else{
// Submit form}


I am really new to php, and databases.. but I'm starting to get the hang of it. Anyways.. the database is set up with a primary key that auto_increments.. so my thought process is;

if( largest primary key < max number of submissions) {
//do above
}

else{
//show error message
}


I think this will work.. but I have no idea on how to do a query to only take the latest submission from the database (and thus the largest primary key).. does anyone know? Also.. what if two people are on the page at once? How can I account for that?

Or does anyone know of a different/easier way to do this?

Thanks!


lavazza
Hi SarahKay,
QUOTE(SarahKay @ Jun 27 2007, 08:30 AM) *

how to do a query to only take the latest submission from the database (and thus the largest primary key).. does anyone know? Also.. what if two people are on the page at once? How can I account for that?

Some ideas:

I'd strongly recommend (or rather, pass on the recommendation of the experts) that you do not use the primary key in ANY logic

Instead, declare another variable - e.g mySubmissionCount - and maybe another called myValidSubmissionCount

This way, if someone makes a mess of a submission, the primary key auto-numbering won't mess up your maxSubmissions logic

And a 'buffer' to warn people that time is running out/its almost full - so don't be surprised if you go thru the whole process only to be told "Sorry, you were too slow... Loser!" wink.gif


How about:


function loadPage(){
//Check to see if submissions are still open
if(maxSubmissions < myValidSubmissionCount) {
showSubmissionForm();
}
else{
displaySubmissionsHaveClosedMessage();
// invite users to provide contact details just in case previous submissions are found to be duplicates or just plain wrong
// sub-task this for re-use below
}
}

function showSubmissionForm() {
if((maxSubmissions - myWarningTrigger) < myValidSubmissionCount) {
displayWarningMessage();
// For the times when there are >1 users accessing the page concurrently
//"Warning: Please you had better be quick... we're almost FULL and it's first up best dressed!"
}
displayInputFields();
validateInput();
}

function validateInput() {
if(maxSubmissions >= myValidSubmissionCount) {
displaySubmissionsHaveClosedMessage();
}
else{
// make sure its all ok
uploadSubmission();
}
}



Maybe...
Brian Chandler
Lavazza's answer appears to be confused - "adding a variable" achieves nothing, since variables are local to each execution of the PHP program. (In other words, a variable can't tell you anything about the state of other calls to the program.)

You can access the highest value of any db field by doing a SELECT as usual, with ORDER BY [field] DESC.

In the end, you can't stop someone getting the form when the campaign starts, and submitting it 3 weeks later - so you just have to respond to a submission with a "Closed" message, as well as removing the submission form from new copies of the relevant page.

If this is a regular thing, you will need another database table for Campaigns, which includes the various status settings - "Open", "Closed", "Almost full" or whatever. Curiously it's one of those things which seems like harder work if you only have to do it once (in which case of course you do as much as possible by hand).

Brian Chandler
QUOTE(lavazza @ Jun 27 2007, 07:47 AM) *


How about:
(open square bracket) color=#999999 (close square bracket)(open square bracket)size=1(close square bracket)



Please don't use this "size" junk. (Is there any chance it could be disabled...?)
lavazza
QUOTE(Brian Chandler @ Jun 27 2007, 04:27 PM) *

Please don't use this "size" junk. (Is there any chance it could be disabled...?)


I was simply following the precedent set in the OP

And as for variables not being stored in a database... that sure is new for me... and probably your bank...

SarahKay
Thanks for the ideas.. I'm definitely going to use the warningTrigger.. I didn't even think about that. :/

As for the variables, like Brian said, they wouldn't do the trick.

Maybe I could set up a second database with one field that gets updated everytime? I could use a query to grab it at the beginning - compare it to the max number of submissions, and when the form has been [successfully] submitted I increase it by one?

Would that work?


Brian Chandler
QUOTE(SarahKay @ Jun 27 2007, 10:30 PM) *

Thanks for the ideas.. I'm definitely going to use the warningTrigger.. I didn't even think about that. :/

As for the variables, like Brian said, they wouldn't do the trick.

Maybe I could set up a second database with one field that gets updated everytime? I could use a query to grab it at the beginning - compare it to the max number of submissions, and when the form has been [successfully] submitted I increase it by one?

Would that work?


Not a second "database", just another database table. But there doesn't seem any point - as you store the submissions you are automatically counting them. But the index of the latest might not be the number of valid submissions if you have a way of deleting invalid ones. So just look up the mysql manual - there's a thingy for counting how many records there are, or how many records meeting some condition (such as acceptstatus = 'Y') without actually fetching them. If you use a separate table you have to make sure it can't get out of sync, and it's always better to design things so there is no "getting out of sync".

SarahKay
er.. yes, sorry by another database I meant another table, sometimes my fingers don't keep up to my brain.

anyways.

QUOTE
So just look up the mysql manual - there's a thingy for counting how many records there are, or how many records meeting some condition (such as acceptstatus = 'Y')


Oh! That's cool! I didn't know I could do that... I will have to find it,

Thanks so much!
SarahKay
I've found it.. for those of you who may be curious;

SELECT COUNT(*) FROM wherever;

Or;

SELECT something, COUNT(*) FROM wherever GROUP BY something;

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.