Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13694 > unrolled thread

How to get the POST name of a field in a form that uses a counter

Started byAdrienne Boswell <arbpen@yahoo.com>
First post2014-05-14 23:22 +0000
Last post2014-05-15 22:45 +0000
Articles 9 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  How to get the POST name of a field in a form that uses a counter Adrienne Boswell <arbpen@yahoo.com> - 2014-05-14 23:22 +0000
    Re: How to get the POST name of a field in a form that uses a counter Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 02:15 +0200
    Re: How to get the POST name of a field in a form that uses a counter Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-15 01:01 +0000
    Re: How to get the POST name of a field in a form that uses a counter Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-15 01:19 +0000
      Re: How to get the POST name of a field in a form that uses a counter The Natural Philosopher <tnp@invalid.invalid> - 2014-05-15 03:29 +0100
    Re: How to get the POST name of a field in a form that uses a counter Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-15 02:55 +0100
    Re: How to get the POST name of a field in a form that uses a counter Arno Welzel <usenet@arnowelzel.de> - 2014-05-15 10:22 +0200
    Re: How to get the POST name of a field in a form that uses a counter Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-15 08:15 -0400
    Re: How to get the POST name of a field in a form that uses a counter Adrienne Boswell <arbpen@yahoo.com> - 2014-05-15 22:45 +0000

#13694 — How to get the POST name of a field in a form that uses a counter

FromAdrienne Boswell <arbpen@yahoo.com>
Date2014-05-14 23:22 +0000
SubjectHow to get the POST name of a field in a form that uses a counter
Message-ID<XnsA32DA68DB274Farbpenyahoocom@78.46.70.116>
I need to how to get the POST name of a field in a form that uses a 
counter.  The fields are named based on that loop. Below is a small 
snippet of what I am doing:

<?php 

$howmuch = array(20, 30, 5, 10, 15);

?>
<form method="post">
<?php 
for($s=0;$s<count($howmuch);++$s)
{?>
<input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo 
$_POST[$howmuch ?]$s; ?>">
<?php } ?>
<input type="submit" value="Submit">
</form>

Here's where my problem is:

When the user hits submit, I would get something in the post array like:

howmuch0 : 20
howmuch1 : 30
howmuch2 : 5
howmuch3 : 10
howmuch4 : 15

On a regular form, value="<?php echo $_POST[$howmuch]; ?>" works just 
fine, but since I'm naming the field in a loop, I need to have that, too.  
Using $_POST[$howmuch ?]$s; is not valid.

I'm sure the answer to this is pretty simple, but I had a hard enough 
time trying to figure out a subject for this post, let alone knowing how 
to google for it.

TIA for your help.  I appreciate it.


-- 
Adrienne Boswell
Arbpen Web Site Design Services - http://www.cavalcade-of-coding.info/
The Good Plate - Fresh Gourmet Recipes - http://the-good-plate.com/
Please respond to the group so others can share

[toc] | [next] | [standalone]


#13695

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-05-15 02:15 +0200
Message-ID<5374071a$0$6620$9b4e6d93@newsspool4.arcor-online.net>
In reply to#13694
Adrienne Boswell wrote:

> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo 
> $_POST[$howmuch ?]$s; ?>">

You can do the following:

  <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo
    $_POST["$howmuch$s"]; ?>">

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#13696

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-05-15 01:01 +0000
Message-ID<ll13kk$998$5@dont-email.me>
In reply to#13694
On Wed, 14 May 2014 23:22:37 +0000, Adrienne Boswell wrote:

> I need to how to get the POST name of a field in a form that uses a
> counter.  The fields are named based on that loop. Below is a small
> snippet of what I am doing:
> 
> <?php
> 
> $howmuch = array(20, 30, 5, 10, 15);
> 
> ?>
> <form method="post">
> <?php for($s=0;$s<count($howmuch);++$s)
> {?>
> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo
> $_POST[$howmuch ?]$s; ?>">

This line gave me an error when I tried it. I don't knwo exactly what you 
meant to write here, but what you wrote isn't valid php.

> <?php } ?>
> <input type="submit" value="Submit">
> </form>

> Here's where my problem is:
> When the user hits submit, I would get something in the post array like:
> howmuch0 : 20 howmuch1 : 30 howmuch2 : 5 howmuch3 : 10 howmuch4 : 15

Loop through the post data using something like (untested):

$vals = array();
foreach ( $_POST as $field => $data ) {
if ( preg_match( "howmuch(\d+)", $field, $bits ) === 1 )
$index = intval( $bits[1] );
$vals[$index] = $data;
}

$vals[X] now contains the data that was in the field $_POST["howmuchX"] 
where X is a sequence of 1 or more digits.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#13698

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-05-15 01:19 +0000
Message-ID<ll14nm$998$6@dont-email.me>
In reply to#13694
On Wed, 14 May 2014 23:22:37 +0000, Adrienne Boswell wrote:

> I need to how to get the POST name of a field in a form that uses a
> counter.

You might also want to look at the following example:

<?php
$h=array(20,30,5,10,15);
if(isset($_POST["h"])){
echo "<ul>\n";
for($s=0;$s<count($h);$s++){
if(isset($_POST["h"][$s]))
echo "<li>\$_POST[\"h\"][{$s}] = {$_POST["h"][$s]}</li>\n";
}
echo "</ul>\n";
}
?>
<form method="post">
<?php
for($s=0;$s<count($h);$s++)
echo "<input type=\"text\" name=\"h[{$s}]\" value=\"{$h[$s]}\">";
?>
<input type="submit" value="Submit">
</form>

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#13700

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-05-15 03:29 +0100
Message-ID<ll18pv$h37$1@news.albasani.net>
In reply to#13698
On 15/05/14 02:19, Denis McMahon wrote:
> On Wed, 14 May 2014 23:22:37 +0000, Adrienne Boswell wrote:
>
>> I need to how to get the POST name of a field in a form that uses a
>> counter.
>
> You might also want to look at the following example:
>
> <?php
> $h=array(20,30,5,10,15);
> if(isset($_POST["h"])){
> echo "<ul>\n";
> for($s=0;$s<count($h);$s++){
> if(isset($_POST["h"][$s]))
> echo "<li>\$_POST[\"h\"][{$s}] = {$_POST["h"][$s]}</li>\n";
> }
> echo "</ul>\n";
> }
> ?>
> <form method="post">
> <?php
> for($s=0;$s<count($h);$s++)
> echo "<input type=\"text\" name=\"h[{$s}]\" value=\"{$h[$s]}\">";
> ?>
> <input type="submit" value="Submit">
> </form>
>
I've used this and it sure works


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#13699

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-05-15 02:55 +0100
Message-ID<0.60075a42d1d9125917e2.20140515025533BST.87vbt7byay.fsf@bsb.me.uk>
In reply to#13694
Adrienne Boswell <arbpen@yahoo.com> writes:

> I need to how to get the POST name of a field in a form that uses a 
> counter.  The fields are named based on that loop. Below is a small 
> snippet of what I am doing:
>
> <?php 
>
> $howmuch = array(20, 30, 5, 10, 15);
>
> ?>
> <form method="post">
> <?php 
> for($s=0;$s<count($howmuch);++$s)
> {?>
> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo 
> $_POST[$howmuch ?]$s; ?>">

  $_POST['howmuch' . $s];

seems to be what you mean.

> <?php } ?>
> <input type="submit" value="Submit">
> </form>

You can, if it helps, get PHP to make $_POST['howmuch'] be a proper
array rather than having five separate numbered elements in $_POST.  You
do this by writing array brackets in the input element's name:

  name="howmuch[<?php echo $s ?>]"

Upon submission, $_POST['howmuch'] will then be an array you can index
like this: $_POST['howmuch'][0] and so on.  Thus you'd write

  value="<?php echo $_POST['howmuch'][$s] ?>"

if you needed to show the value entered.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#13709

FromArno Welzel <usenet@arnowelzel.de>
Date2014-05-15 10:22 +0200
Message-ID<53747957.404@arnowelzel.de>
In reply to#13694
Am 15.05.2014 01:22, schrieb Adrienne Boswell:

> I need to how to get the POST name of a field in a form that uses a 
> counter.  The fields are named based on that loop. Below is a small 
> snippet of what I am doing:
> 
> <?php 
> 
> $howmuch = array(20, 30, 5, 10, 15);
> 
> ?>
> <form method="post">
> <?php 
> for($s=0;$s<count($howmuch);++$s)
> {?>
> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo 
> $_POST[$howmuch ?]$s; ?>">
> <?php } ?>
> <input type="submit" value="Submit">
> </form>
> 
> Here's where my problem is:
> 
> When the user hits submit, I would get something in the post array like:
> 
> howmuch0 : 20
> howmuch1 : 30
> howmuch2 : 5
> howmuch3 : 10
> howmuch4 : 15
> 
> On a regular form, value="<?php echo $_POST[$howmuch]; ?>" works just 
> fine, but since I'm naming the field in a loop, I need to have that, too.  
> Using $_POST[$howmuch ?]$s; is not valid.

foreach() should be your friend.



-- 
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de

[toc] | [prev] | [next] | [standalone]


#13714

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-05-15 08:15 -0400
Message-ID<ll2b4m$qnr$1@dont-email.me>
In reply to#13694
On 5/14/2014 7:22 PM, Adrienne Boswell wrote:
> I need to how to get the POST name of a field in a form that uses a
> counter.  The fields are named based on that loop. Below is a small
> snippet of what I am doing:
>
> <?php
>
> $howmuch = array(20, 30, 5, 10, 15);
>
> ?>
> <form method="post">
> <?php
> for($s=0;$s<count($howmuch);++$s)
> {?>
> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo
> $_POST[$howmuch ?]$s; ?>">
> <?php } ?>
> <input type="submit" value="Submit">
> </form>
>
> Here's where my problem is:
>
> When the user hits submit, I would get something in the post array like:
>
> howmuch0 : 20
> howmuch1 : 30
> howmuch2 : 5
> howmuch3 : 10
> howmuch4 : 15
>
> On a regular form, value="<?php echo $_POST[$howmuch]; ?>" works just
> fine, but since I'm naming the field in a loop, I need to have that, too.
> Using $_POST[$howmuch ?]$s; is not valid.
>
> I'm sure the answer to this is pretty simple, but I had a hard enough
> time trying to figure out a subject for this post, let alone knowing how
> to google for it.
>
> TIA for your help.  I appreciate it.
>
>

I think you're going about this the wrong way.  You should use a two 
dimensional array for your value; it will be much easier in the long run.

Your input statement should look like:

<input type="text" name="howmuch[]"... >

Then your $_POST elements would be $_POST['howmuch'][0], 
$_POST['howmuch'][1], etc.

You can also then use foreach($_POST['howmuch'] as ...)

to go through the 'howmuch' elements in the $_POST array.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13734

FromAdrienne Boswell <arbpen@yahoo.com>
Date2014-05-15 22:45 +0000
Message-ID<XnsA32EA04DF1D61arbpenyahoocom@78.46.70.116>
In reply to#13694
Adrienne Boswell <arbpen@yahoo.com> wrote

> I need to how to get the POST name of a field in a form that uses a 
> counter.  The fields are named based on that loop. Below is a small 
> snippet of what I am doing:
> 
> <?php 
> 
> $howmuch = array(20, 30, 5, 10, 15);
> 
> ?>
> <form method="post">
> <?php 
> for($s=0;$s<count($howmuch);++$s)
> {?>
> <input type="text" name="howmuch<?php echo $s; ?>" value="<?php echo 
> $_POST[$howmuch ?]$s; ?>">
> <?php } ?>
> <input type="submit" value="Submit">
> </form>
> 
> Here's where my problem is:
> 
> When the user hits submit, I would get something in the post array
> like: 
> 
> howmuch0 : 20
> howmuch1 : 30
> howmuch2 : 5
> howmuch3 : 10
> howmuch4 : 15
> 
> On a regular form, value="<?php echo $_POST[$howmuch]; ?>" works just 
> fine, but since I'm naming the field in a loop, I need to have that,
> too.  Using $_POST[$howmuch ?]$s; is not valid.
> 
> I'm sure the answer to this is pretty simple, but I had a hard enough 
> time trying to figure out a subject for this post, let alone knowing
> how to google for it.
> 
> TIA for your help.  I appreciate it.
> 
> 

Thanks to everyone for the suggestions.  I wound up using a rather hinky 
method, but it works, so I'm happy.  I will be posting the page when I 
have it up on the server so y'all can see it.

-- 
Adrienne Boswell
Arbpen Web Site Design Services - http://www.cavalcade-of-coding.info/
The Good Plate - Fresh Gourmet Recipes - http://the-good-plate.com/
Please respond to the group so others can share

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web