Incrementing a counter and displaying content based on counter value

Posted on 2009-02-16
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
Question by:ShaneJones
LVL 15

Accepted Solution

fsze88 earned 500 total points
ID: 23649093
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

LVL 16

Assisted Solution

sunithnair earned 500 total points
ID: 23649154
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)

Assisted Solution

Fero45 earned 500 total points
ID: 23649198
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

LVL 15

Expert Comment

ID: 23649244
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....
LVL 49

Assisted Solution

Roonaan earned 500 total points
ID: 23654394
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question