Incrementing a form submission counter

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

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

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

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