JarJarJoe has asked for the wisdom of the Perl Monks concerning the following question:

good day monks , I need to create a button that will increment value. I am lost. need help. please

basically submit will increment the value $var by 1

need help and guidance , i am a newbie

#!C:\strawberry\perl\bin\perl.exe use CGI::Session; use CGI; use CGI":standard"; use CGI::Carp qw(fatalsToBrowser); $| = 1; print header; print start_html; my $cgi = new CGI; my $Session = new CGI::Session( undef, $cgi, {Directory=>'/tmp'} ); $var =0; $limit=100; $submit = param("Submit"); print <<HTML; <form> <input type="submit" name="Submit" value="add" ><br/></input> </form> HTML if ($submit eq 'add'){ print "var is".$var; if ($var < $limit){ $var++; print "add".$var; $Session->{'MyVar'} = $var; print "value is".$var; $var = $Session->{'MyVar'}; print "retrieve".$var."\n"; print "<br/>\n"; } else{ } }else{} print end_html;

Replies are listed 'Best First'.
Re: Submit button increment value
by davido (Cardinal) on Jan 28, 2014 at 19:21 UTC

    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

Re: Submit button increment value
by SuicideJunkie (Vicar) on Jan 28, 2014 at 17:42 UTC

    Your post is a bit broken up, since you have <code></code> tags inside your code. Use the alternate <c></c> code tags around sections that have </code> literally in them.

    When do you expect that code to run? When the user sees the page, all the perl code has already been run. When the user clicks submit, the browser fetches a new page with the form's parameters passed to that new page. So any code you want to be run on the submit click should be placed in that other page. Or if you want to run it in the user's browser before they submit the form, then you probably want some javascript.

    There should be some monks by shortly with a pile of CGI & documentation links for understanding what's really going on.