We help IT Professionals succeed at work.

Incrementing a counter and displaying content based on counter value

ShaneJones asked
Medium Priority
Last Modified: 2013-11-10
I have 5 links and when a user clicks on them I want to increment a counter in the session (without changing the URL.)

When the session counter reached 5 I want a div to be created on the fly

ANyone know how I can increment the counter in the session on click without changing the page URL
Watch Question

click on a link and increment a counter in the session ?
In my mind, you should increment a counter in cookie by javascript...
and it is easier to count the counter from cookies by javascript.
If you want to save counter in session?
Could you save it on next page by reading from cookies to session on php pages?
It would be a easy way to do you goal and/or div to be created on the fly.. (without changing the page URL)
If I mention suit for your demand. please have a look set/get cookies by javascript http://www.w3schools.com/JS/js_cookies.asp

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
You can change the session variable but you will need to submit (postback) the form (this does not require changing the url) and increament the session counter when the page is posted back. If you donot wish to submit the form you could do it using the cookies method mentioned in the 1st post or with the help of javascript variables. (storing the cound in js variable)
You can use session, If you click on 'My link', the counter should increment by one. try this
if( !$_SESSION['counter'] )	{
    $_SESSION['counter'] = 1;
} else {
    $_SESSION['counter'] = $_SESSION['counter'] + 1;
  echo $_SESSION['counter'];
<a href='test.php'>My link</a>

Open in new window


Other method, create a from in iframe tag to be hidden. pass your master form value to iframe tag and submit.
BUT, I am not suggest to do that....
Top Expert 2006
You could use jQuery, then use:
<script type="text/javascript" src="path/to/jquery-1.2.6-min.js"></script>

<div id="counter"><?php echo $_SESSION['counter']; ?></div>

<a onclick="$('#counter').load('updatecounter.php');return false;" href="#">Click me</a>

updatecounter.php then only needs to update the session counter and only return:
echo $_SESSION['counter'];
as its sole output.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.