virken Posted December 20, 2006 Share Posted December 20, 2006 Thanks to those who helped previously - you guys are awesome!One more question... I need to increment a session variable but only on form submission. Using the code below, my variable gets incremented every time the page loads. How can I make a variable incrementing only when a form is submitted and not during page refreshes and back navigation?<input name="total" type="hidden" value=" <?php $_SESSION['total']++;?> "> Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/ Share on other sites More sharing options...
JasonLewis Posted December 20, 2006 Share Posted December 20, 2006 if you have a submit button give it a name, like submit then just check if it has been pressed.[code=php:0]<?phpif(isset($_POST['submit'])){echo "<input name='total' type='hidden' value='".$_SESSION['total']++."'>";}?><input type="text" name="afield"><br><input type="submit" name="submit" value="go">[/code]of course thats just an example... Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-144893 Share on other sites More sharing options...
virken Posted December 20, 2006 Author Share Posted December 20, 2006 hmm... please take a look at this; tried to implement your suggestion but must have gone off-track...<?php session_start(); if ( ! session_is_registered( $_SESSION['total']) ) {$_SESSION['total'] = 0; $total = $_POST['total'];} ?><html>Increment Test<?php echo "Pre-increment value of total = ", $_SESSION['total']; ?><form name="form1" method="post" action= "<?php $_SERVER['PHP_SELF']?>"><input type="submit" name="Submit" value="Submit"><?php if(isset($_POST['submit'])){ echo "<input name='total' type='hidden' value='".$_SESSION['total']++."'>"; }?></form><?php echo "Post-increment value of total = ", $_SESSION['total']; ?></html> Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-144913 Share on other sites More sharing options...
kenrbnsn Posted December 20, 2006 Share Posted December 20, 2006 You don't want to use session_is_registered(). You want to check to see if a session variable is set:[code]<?phpif ( !isset($_SESSION['total']) ) { $_SESSION['total'] = 0; $total = $_POST['total'];}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-144926 Share on other sites More sharing options...
virken Posted December 20, 2006 Author Share Posted December 20, 2006 understood and fixed that - but the problem seems to be in the php in the form.There I check to see - if(isset($_POST['submit'] - but it must not be true because the increment never occurs??? Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145161 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Should have a capital "S" (like in your form):if(isset($_POST['[b][color=red]S[/color][/b]ubmit']))Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145164 Share on other sites More sharing options...
virken Posted December 20, 2006 Author Share Posted December 20, 2006 thank you for helping me clean up the syntax - unfortunately the original problem remains. Whenever you REFRESH the page, the variable continues to increment. I need to prevent the increment unless the user has actually submitted the form - any other ideas???<?php session_start(); if ( ! (isset( $_SESSION['total']) )) { $_SESSION['total'] = 0; $total = $_POST['total']; }?><html>Increment Test<?php echo "Pre-increment value of total = ", $_SESSION['total']; ?><form name="form1" method="post" action= "<?php $_SERVER['PHP_SELF']?>"><input type="submit" name="Submit" value="Submit"><?php if(isset($_POST['Submit'])){ echo "<input name='total' type='hidden' value='".$_SESSION['total']++."'>"; }?></form><?php echo "Post-increment value of total = ", $_SESSION['total']; ?></html> Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145220 Share on other sites More sharing options...
JasonLewis Posted December 20, 2006 Share Posted December 20, 2006 are you sure? i tested it on mine and it works. i refresh before submitting the form and it dosnt go up but when i submit it goes up. but if you refresh after you submit then it will go up because its still got the post data. Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145495 Share on other sites More sharing options...
Daniel0 Posted December 20, 2006 Share Posted December 20, 2006 I didn't read the entire topic, but if you instead of $_SESSION['total']++ you could use $_SESSION['total']+1. That will not change the variable, but still output it one higher than $_SESSION['total']. Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145497 Share on other sites More sharing options...
virken Posted December 21, 2006 Author Share Posted December 21, 2006 That's the issue since i flip/flop between two pages (a question page and an answer page).Is it possible increment a variable with a function, or to purge the POST data so the variable only gets incremented once when the form is originally submitted? Quote Link to comment https://forums.phpfreaks.com/topic/31308-stop-variable-incrementing/#findComment-145842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.