FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How to get the POST name of a field in a form that uses a counter
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
How to get the POST name of a field in a form that uses a counter [message #185931] Wed, 14 May 2014 23:22 Go to next message
Adrienne Boswell is currently offline  Adrienne Boswell
Messages: 25
Registered: October 2010
Karma: 0
Junior Member
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
Re: How to get the POST name of a field in a form that uses a counter [message #185932 is a reply to message #185931] Thu, 15 May 2014 00:15 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
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
Re: How to get the POST name of a field in a form that uses a counter [message #185933 is a reply to message #185931] Thu, 15 May 2014 01:01 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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(at)gmail(dot)com
Re: How to get the POST name of a field in a form that uses a counter [message #185935 is a reply to message #185931] Thu, 15 May 2014 01:19 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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(at)gmail(dot)com
Re: How to get the POST name of a field in a form that uses a counter [message #185936 is a reply to message #185931] Thu, 15 May 2014 01:55 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
Adrienne Boswell <arbpen(at)yahoo(dot)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.
Re: How to get the POST name of a field in a form that uses a counter [message #185937 is a reply to message #185935] Thu, 15 May 2014 02:29 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.
Re: How to get the POST name of a field in a form that uses a counter [message #185946 is a reply to message #185931] Thu, 15 May 2014 08:22 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
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
Re: How to get the POST name of a field in a form that uses a counter [message #185951 is a reply to message #185931] Thu, 15 May 2014 12:15 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: How to get the POST name of a field in a form that uses a counter [message #185972 is a reply to message #185931] Thu, 15 May 2014 22:45 Go to previous message
Adrienne Boswell is currently offline  Adrienne Boswell
Messages: 25
Registered: October 2010
Karma: 0
Junior Member
Adrienne Boswell <arbpen(at)yahoo(dot)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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Message Subject
Next Topic: query: how many use PHP for linux scripts
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 20:34:54 GMT 2024

Total time taken to generate the page: 0.02516 seconds