Forum Moderators: coopster & phranque

Message Too Old, No Replies

Retrieving form values in a loop

Need help

         

emunir

7:01 am on Jul 7, 2003 (gmt 0)

10+ Year Member



Hi,

I'm trying to get the user to add certain info and store it like this

$heading1, $heading2, $heading3

I have created a loop like this:
for($i=1; $i< $sections; $i++) {
<input name="heading$i" type="text" >
}

Now the problem is that when I try to print the same info via a loop it only prints the numbers.

I have created a loop like this:
for($i=1; $i< $sections; $i++) {
print "$heading$i";
}

What do I need to do so that it prints the values for $heading1, $heading2 and so on in the loop.

Thanx!

netcommr

8:18 am on Jul 7, 2003 (gmt 0)

10+ Year Member



this may do it for ya
------------------------


use CGI;

my $c = new CGI;

my ($x,$y);

# loop over each form input name
foreach $x($c->param) {

# skip if key not evaluate to 'heading'+(1 or more digits)
next if $x!~ /^heading\d+$/;

# put value in scalar context
$y = $c->param($x);

print $y; # print value
}

[edited by: netcommr at 8:26 am (utc) on July 7, 2003]

Nick_W

8:21 am on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi and welcome to WebmasterWorld! [webmasterworld.com]

Here's what you need.

Variable variables [php.net]

Nick

netcommr

8:25 am on Jul 7, 2003 (gmt 0)

10+ Year Member



which language is it?
perl ¦¦ php

add
-------

must B perl in this category...

[edited by: netcommr at 8:36 am (utc) on July 7, 2003]

Nick_W

8:25 am on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Damn! Good point.... ;)

Nick

netcommr

8:28 am on Jul 7, 2003 (gmt 0)

10+ Year Member



BTW, my line starting with 'next if $x' needs a space between the $x and the!~ (space seems to get erased in posting everytime...)

emunir

10:37 am on Jul 7, 2003 (gmt 0)

10+ Year Member



Thanx so much! Used the array option and it worked.
It was Perl btw.