Counting Button Using PHP

Discussion in 'PHP' started by Ipodman79, May 23, 2014.

  1. #1
    Hi Guys,

    I was wondering how to make this script work. I want it to add 1 to a variable every time the button is clicked. I already have,

    
    <?php
    session_start();
    
    // if your variable is not yet defined, you assigned it with 0
    if (isset($_SESSION['count']))
    {
        $_SESSION['count'] = 0;
    }
    
    if($_POST['submit'])
    {
        $_SESSION['count'] = $_SESSION['count'] + 1;
        echo $_SESSION['count'];
        ++$_SESSION['count'];  
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Bale Counter</title>
    </head>
    <body>
    <form action="index.php" method="post">
        <input type="submit" name="submit" value="Bale Out">
    </form>
    </body>
    </html>
    
    PHP:
    How do I make this work?
     
    Solved! View solution.
    Ipodman79, May 23, 2014 IP
  2. #2
    Hi.
    Try the following modified code:
    <?php
    session_start();
    
    // if your variable is not yet defined, you assigned it with 0
    if (!isset($_SESSION['count']))
    {
        $_SESSION['count'] = 0;
    }
    
    if(isset($_POST['submit']))
    {
        $_SESSION['count']++;
        echo $_SESSION['count'];
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Bale Counter</title>
    </head>
    <body>
    <form action="index.php" method="post">
        <input type="submit" name="submit" value="Bale Out">
    </form>
    </body>
    </html>
    PHP:
     
    2WDH.com, May 23, 2014 IP
  3. thewpdev

    thewpdev Well-Known Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Do you want to save that value for future reference as wel or just want that for a particular session?
     
    thewpdev, Jun 5, 2014 IP
  4. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #4
    No, I want it to re-set when a reset button is pressed.
     
    Ipodman79, Jun 6, 2014 IP
  5. thewpdev

    thewpdev Well-Known Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    I hope you know that session variable will expire within 30 to 180 mins depending on your PHP installation.
     
    thewpdev, Jun 6, 2014 IP
  6. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    Yes I agree with above solution..
    You need to use same solution for that it definetly works.
     
    vruvishal, Jun 8, 2014 IP
  7. Vibrantu

    Vibrantu Greenhorn

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #7
    You should replace your following code

    if (isset($_SESSION['count']))
    {
    $_SESSION['count'] = 0;
    }

    with

    if (!isset($_SESSION['count']))
    {
    $_SESSION['count'] = 0;
    }
     
    Vibrantu, Jun 8, 2014 IP
  8. Dev_Navi

    Dev_Navi Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    Save the variable's updated value in the database.
     
    Dev_Navi, Jun 9, 2014 IP