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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Wed Sep 18 13:17:01 GMT 2024

Total time taken to generate the page: 0.06124 seconds