go ahead... be a heretic PerlMonks

### Increment Number with form button push

by peppiv (Curate)
 on Jun 13, 2002 at 19:34 UTC Need Help??

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

Problem: I'm trying to increment the value of a variable with the push of a form button. I would like the value to increment by 10 every time I push the button. I'm apparently brain dead today because I can't figure this simple problem out. Please help!

I think we can do this with only a snippet.

```my \$limit_3 = param("limit_3");
my \$limit = param("limit");
my \$add_limit = (\$limit_3 + \$limit);
my \$limit = (\$limit_3 + 0);

print qq(\$limit);

+ name="limit_3" value="10"><input type="hidden" name="limit" value="\$
+limit"><input type="submit" name="action" value="Next"></form>

\$limit starts at 0 - Perfect!
One push of the button and \$limit = 10. - Perfect again!
Another push of the button and \$limit = 20. Again Perfect!
Yet another push of the button and \$limit still = 20.
Every subsequent push and the value continues to remain at 20. I would like it to increment by 10 EVERY time I push the button. Where am I failing?

peppiv

if (\$date = \$today) {\$brain = \$mush}

Replies are listed 'Best First'.
(jeffa) Re: Increment Number with form button push
by jeffa (Bishop) on Jun 13, 2002 at 19:48 UTC
You are never updating the variable to be carried over. First, \$add_limit is unecessary, just do this instead:
```my \$limit_3 = param("limit_3");
my \$limit = param("limit");
\$limit = (\$limit_3 + \$limit);

print \$limit; # and rest of form
Now, when you write out the hidden field, it will contain the current value. Before, you were writing out \$limit to the hidden field without changing it's initial value, which was always 0.

Here is some more code to meditate upon:

```use strict;
use CGI qw(:standard);

my \$i = param('hidden_i') || 0;
my @view = qw(
first second third   fourth
fifth sixth  seventh eighth
);

my \$view = \$view[\$i++];

param('hidden_i',\$i % @view);

start_html('hidden fields'),
h1(\$view),
start_form,
hidden('hidden_i'),
submit,
end_form,
end_html,
;

jeffa

```L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
```
Ah Yes! This works!
If I wasn't having such a brain fart I'd write something clever.

Thanx

peppiv

Re: Increment Number with form button push
by SarahM (Monk) on Jun 13, 2002 at 19:43 UTC
I'm not sure what you are trying to do... you declare \$limit twice. And you never use \$add_limit, which looks like the variable you would want. Is there more code, or am I just missing something?
Update

My mistake! I meant:

peppiv
Then you need to change \$limit in the form to \$add_limit as well!

Create A New User
Node Status?
node history
Node Type: perlquestion [id://174310]
Approved by ichimunki
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-06-19 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What does the "s" stand for in "perls"? (Whence perls)

Results (93 votes). Check out past polls.

Notices?