Incrementing a form submission counter

5 posts by 2 authors in: Forums > CMS Builder
Last Post: October 26, 2012   (RSS)

By gkornbluth - October 26, 2012

Hi all,

Probably something simple (or dumb)...

I’d like to be able to increment a variable that counts how many times a form’s submit button has been clicked, but I can’t seem to get the counter to increment past 1.

I’ve inserted this field into the form:
<input type="hidden" name="submit_count" value= "1" id="submit_count"/>

And then in the body, this code:
<?php if (@$_REQUEST['submit_count'] == "1" ): ?>
<?php $submit_count++; ?>
<?PHP endif ?>
<br />This form has been submitted <?php echo $submit_count ?> times.<br />


Any ideas appreciated.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Incrementing a form submission counter

By gregThomas - October 26, 2012

Hi Jerry,

I've just done a test and this method seemed to work well:

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }


if(@isset($_REQUEST['submit_count'])){
$count = $_REQUEST['submit_count'] + 1;
}else{
$count = 0;
}

?>

<form action="" method="post">
<input name="submit_count" type="hidden" value="<?php echo $count; ?>" />
<input type="submit" name="submit1" value="submitted" />
</form>

<p>You have submitted this form: <?php echo $count; ?> times!</p>


The only problem with this method is that if the user refreshes the page then the count will be lost. You could look into storing the count integer in a session if this is a problem.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] Incrementing a form submission counter

By gkornbluth - October 26, 2012

Hi Greg,

Thanks for getting back on this.

For this particular situation page refreshing is not an issue since in this case it's only to manage some redundant information displays on the page.

It would be great to learn how to store the integer in a session for other uses and to inform other users.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [greg] Incrementing a form submission counter

By gkornbluth - October 26, 2012

Thanks Greg,

Haven't played with the session yet, but the other works perfectly...

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php