Samuz Posted November 21, 2011 Share Posted November 21, 2011 Hi guys, i'm trying to increment a variable / session value whenever 'submit' comes in the POST. I can't seem to get around the logic of this. Code looks like this: $var = 0 if($_POST('submit'){ $_SESSION['var2'] = $var++; } What I want it to do is increment the value of the session index everytime a user clicks 'submit'. But because i've reset $var to 0 everytime, it's always just going to stay at 1. I just can't think of a way to achieve this, does anyone have any clue on how I can? Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/ Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 $_SESSION['var2']++; Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290011 Share on other sites More sharing options...
Samuz Posted November 21, 2011 Author Share Posted November 21, 2011 $_SESSION['var2']++; Yeah that's a problem. Because $var is still being reset back to 0 everytime and so will the session variables.. Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290028 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 why are you using $var ?? you said "What I want it to do is increment the value of the session index everytime a user clicks 'submit'."... If you're storing this in a SESSION, each user will have his own value... (each user has his own session). If your goal is to have a global count of how many times the form was submitted, you need to store it somewhere else (database of .txt file or whatever)... it's basically the same as a page counter, but only activated on form submission... Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290031 Share on other sites More sharing options...
Samuz Posted November 21, 2011 Author Share Posted November 21, 2011 why are you using $var ?? you said "What I want it to do is increment the value of the session index everytime a user clicks 'submit'."... If you're storing this in a SESSION, each user will have his own value... (each user has his own session). If your goal is to have a global count of how many times the form was submitted, you need to store it somewhere else (database of .txt file or whatever)... it's basically the same as a page counter, but only activated on form submission... Because the counter has to start from somewhere (0 in this example). I was thinking about just going ahead and incrementing it with it without defining the starting point, but i'm not sure that will work because i'm sure PHP will return an undefined variable error. right? Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290036 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 in that case just check if it exists first: if(!isset($_SESSION['var2'])){ $_SESSION['var2'] = 1; }else{ $_SESSION['var2']++; } Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290043 Share on other sites More sharing options...
Samuz Posted November 21, 2011 Author Share Posted November 21, 2011 in that case just check if it exists first: if(!isset($_SESSION['var2'])){ $_SESSION['var2'] = 1; }else{ $_SESSION['var2']++; } Thanks, makes perfect sense. Quote Link to comment https://forums.phpfreaks.com/topic/251546-small-increment-logical-problem/#findComment-1290120 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.