Counting button clicks:

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antoni

    Counting button clicks:

    Hello,

    I was trying to write a simple shopping example, which allows the user
    to select two poducts and we kept a counter of each item. In this
    code, the sessions are created, but the files value does not increase.

    I would appreciate some guidance.

    Thank you

    <?php
    session_save_pa th("c:");
    session_registe r("item1");
    session_registe r("item2");
    ?>

    <?php
    if(isset($_GET['Item1'])){
    $GLOBALS["item1"]++;
    }

    if(isset($_GET['Item2'])){
    $GLOBALS["item2"]++;
    }
    ?>

    <html>
    <head>
    <title>Test Page</title>
    </head>

    <body>
    <form>
    <input type="submit" name="Item1" value="Item 1">
    <input type="submit" name="Item2" value="Item 2">
    </form>
    </body>

    </html>
  • Pedro Graca

    #2
    Re: Counting button clicks:

    Antoni wrote:[color=blue]
    > I was trying to write a simple shopping example, which allows the user
    > to select two poducts and we kept a counter of each item. In this
    > code, the sessions are created, but the files value does not increase.
    >
    > I would appreciate some guidance.
    >
    > Thank you
    >
    > <?php
    > session_save_pa th("c:");[/color]

    session_save_pa th('c:\\'); # I don't like "c:" :)
    # better yet, IMHO, is to configure php.ini and never worry about it

    # You never started the session
    session_start() ;
    [color=blue]
    > session_registe r("item1");
    > session_registe r("item2");[/color]

    # session_registe r() and register_global s=Off in php.ini seem to not
    # work well. I never used session_registe r(). I'd do that as

    if (!isset($_SESSI ON['item1'])) $_SESSION['item1'] = 0;
    if (!isset($_SESSI ON['item2'])) $_SESSION['item2'] = 0;
    [color=blue]
    > ?>
    >
    > <?php
    > if(isset($_GET['Item1'])){
    > $GLOBALS["item1"]++;[/color]

    # Use the session variable directly
    $_SESSION['item1']++;
    [color=blue]
    > }
    >
    > if(isset($_GET['Item2'])){
    > $GLOBALS["item2"]++;[/color]

    $_SESSION['item2']++;
    [color=blue]
    > }
    > ?>
    >
    > <html>
    > <head>
    > <title>Test Page</title>
    > </head>
    >
    > <body>
    > <form>
    > <input type="submit" name="Item1" value="Item 1">
    > <input type="submit" name="Item2" value="Item 2">
    > </form>[/color]

    <!-- you don't ever display the current click count -->
    <p>Number of Item 1 clicks: <?php echo $_SESSION['item1']; ?></p>
    <p>Number of Item 2 clicks: <?php echo $_SESSION['item2']; ?></p>
    [color=blue]
    > </body>
    >
    > </html>[/color]


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Antoni

      #3
      Re: Counting button clicks:

      Hello,

      I changed the code, and looked again at the session files. Though I
      notieced all the files included item1|i:0;item2 |i:0;

      My php text includes an example using the (SID) tool with session,
      would you recommend using this tool?

      Thank you

      <?php
      session_save_pa th('c:\\');
      session_start() ;
      if (!isset($_SESSI ON['item1'])) $_SESSION['item1'] = 0;
      if (!isset($_SESSI ON['item2'])) $_SESSION['item2'] = 0;

      if(isset($_GET['Item1'])){
      $_SESSION['item1']++;
      }
      if(isset($_GET['Item2'])){
      $_SESSION['item2']++;
      }
      ?>

      <html>
      <head>
      <title>Test Page</title>
      </head>

      <body>

      <form>
      <input type="submit" name="Item1" value="Item 1">
      <input type="submit" name="Item2" value="Item 2">

      <p>Number of Item 1 clicks: <?php echo $_SESSION['item1']; ?></p>
      <p>Number of Item 2 clicks: <?php echo $_SESSION['item2']; ?></p>
      </form>

      </body>
      </html>

      Comment

      • Pedro Graca

        #4
        Re: Counting button clicks:

        Antoni wrote:[color=blue]
        > I changed the code, and looked again at the session files. Though I
        > notieced all the files included item1|i:0;item2 |i:0;
        >
        > My php text includes an example using the (SID) tool with session,
        > would you recommend using this tool?[/color]

        I guess your problem is that the webserver doesn't have permission to
        change the session files. Try showing all errors, warnings, and notices
        when you run your example.
        [color=blue]
        > Thank you
        >
        > <?php[/color]

        ### add these two lines to your script
        ### right after the php opening tag
        error_reporting (E_ALL);
        ini_set('displa y_errors', '1');

        (snip code that works for me ...)

        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        Working...