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

By gregThomas - October 26, 2012

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

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