I went ahead and fixed your code formatting. The proper way to format code is to place a <code> tag on the line before your code, then paste all your code (with proper 0x20-based indentation), then place a </code> tag on the line following all of your code. What you shouldn't be doing is placing <br/><code> line of code </code> for each line of your code. Formatting is lost, mistakes are made, and it becomes a lot of work to fix.

Now for your "problem". You're setting $var = 0 each time the script is run. But you're not reading the session back in and using it to set $var to the value previously set in the session before incrementing it again. That means that when a user hits submit, you increment $var from 0 to 1, and you save it to the session, but you don't retrieve it prior to incrementing, so subsequent runs will always start at zero again.

CGI is stateless, and you must be aware of that or you wouldn't be using the CGI::Session module. But it really is stateless: You have to expect that when submit is clicked, the script is re-run from the start as though it never ran before. To maintain state, you need to be retrieving the appropriate session and setting $var to the previous state before incrementing it.

Also, you shouldn't over-complicate your code by adding else{} clauses to all of your if statements, unless there's something for the else{} clause to do. There may be times when adding them increases readability, but as it stands, they detract from it, IMO.


Dave


In reply to Re: Submit button increment value by davido
in thread Submit button increment value by JarJarJoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.