Incrementing a form submission counter

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

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

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: [gkornbluth] Incrementing a form submission counter

Hi Jerry,

I would use something like this:

<?php

// 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."); }
session_start();
if(!isset($_SESSION['submit_count']) || empty($_SESSION['submit_count'])){
$_SESSION['submit_count'] = 0;
}

if(isset($_REQUEST['submit_count'])){
$_SESSION['submit_count'] = $_SESSION['submit_count'] + 1;
}
showme($_SESSION);
?>

<form action="" method="post">
<input name="submit_count" type="hidden" value="submitted" />
<input type="submit" name="submit1" value="submitted" />
</form>

<p>You have submitted this form: <?php echo $_SESSION['submit_count']; ?> times!</p>


The first if statement checks if the submit_count value has been set to a session, if not it creates it.

The second if statement checks if the form has been submitted and adds a 1 to the session submit_count variable if it has.

Thanks
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] Incrementing a form submission counter

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