Incrementing a counter and displaying content based on counter value

Posted on 2009-02-16
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

    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

    LVL 16

    Assisted Solution

    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)
    LVL 6

    Assisted Solution

    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

    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

    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

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now