Re: $_POST variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Kirk,

On Saturday, March 12, 2011, 4:09:24 AM, you wrote:

> On 3/11/2011 2:43 PM, Geoff Lane wrote:

> [snip]
>> You could use foreach to iterate through the post variables until you
>> encounter a match:
>>
>> foreach ($_POST as $key =>  $value){
>>      if (substr($key, 0, 6) == "radio_") {
>>         $buttonName = $key;
>>         $buttonValue = 4value;
>>         break 2;
>>      }
>> }
>>
>> I haven't tried the above code, but I hope someone will correct my
>> efforts if I'm wrong.
>>
> ok, now I am very new to php, so if i got this wrong be nice.

> It APPEARS TO ME that you are setting a variable called buttonName 
> to the extracted value stored in $key for each name in the post 
> submission, and a variable named buttonValue for the item's value. 
> THEM, you do the same thing again to the same destination variables 
> for the next name/value pair, and so-on until they list of 
> name/value pairs is exhausted. IF this understanding is correct, 
> only the LAST name/value pair will emerge from the process intact; 
> prior values will be obliterated. Would they not be better to append 
> them to a single dimensioned array, which starts life as a null 
> array? If I am getting this wrong, please administer wet mackerel 
> therapy to my tired head and explain the facts.

FWIW, I'm fairly new 'on the second time around' as most of my work
was in other scripting/programming languages for about ten years and I
only returned about a month ago having forgotten most of the PHP I
once knew. Accordingly, Richard Gray administered the soggy kipper to
me for using "break 2". I'd wrongly assumed that 'break' on its own
would only have exited the 'if' construct (just as it exists
'switch'). So the last line inside the if construct should have been
'break' rather than 'break 2'.

Returning to my code, we're looping through each of the $_POST
variables looking for one with a name that starts with 'radio_'. On
finding that, we set $buttonName to the name of that $_POST variable,
set $buttonValue to the its value, and then the break statement stops
further processing of the $_POST variable array. This means that we
exit the foreach loop with $buttonName and $buttonValue set to the
*first* "radio_xx" $_POST variable. Without the 'break' the code
returns the last "radio_xx" as you suggested.

However, the OP asked merely how to identify the name of a $_POST
variable when only part of its name was known, and that's the question
most of us who responded addressed. It's then up to the OP to adapt
the code to his or her own use (for example, by carrying out whatever
was needed with each key and value pair inside the foreach loop).

HTH,

-- 
Geoff Lane
Cornwall, UK


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux