Re: [PHP] Re: $_POST variable
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. given your code example - 'break 2;' -- s/b just 'break;' ... 'break 2;' is to exit the outer loop of a nested loop which is not the case here. Rich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: $_POST variable
On 3/11/2011 12:03 PM, Shawn McKenzie wrote: On 03/11/2011 01:28 PM, Danny wrote: Hi guys, I have a form that has a long list of radio-bottons inside of it. The radio-buttons are dynamically created via php and MySQL. Here is an example of one of the radio buttons: input type=radio name=?php print (radio_.$result_from_mysql) ; ? value=0 input type=radio name=?php print (radio_.$result_from_mysql) ; ? value=1 Now, when I submit this form to another page for processing, how would I catch the above radio-button's $_POST name since I do not know the name, only that it starts with radio_ ? Thank You Danny The most common and flexible way to do this sort of thing is to use arrays instead: input type=radio name=radio[?php echo $result_from_mysql; ?] value=0 input type=radio name=radio[?php echo $result_from_mysql; ?] value=1 Then: foreach($_POST['radio'] as $key = $value) { echo radio for $key is $value; } Your example would be good if the OP wanted checkbox'es. But with radio buttons, the whole point (most of the time) is to have the form only allow you to have one of the radio input fields selected at any given time. How you showed it, it would not see the uniqueness of the radio button names, and therefor allow more than one of the radio input fields to be selected at a time. I would try something like this: As long as this is correct: input type=radio name=radio_?php echo $result_from_mysql; ? value=0 /Zero input type=radio name=radio_?php echo $result_from_mysql; ? value=1 /One Then I would do the following: foreach ($_POST as $k = $v) { if ( strpos(trim($k), 'radio_') === 0 ) { echo $k.' is a match, and it\'s value is '.$v.'.br/'.PHP_EOL; } } Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: $_POST variable
On 03/11/2011 02:33 PM, Jim Lucas wrote: On 3/11/2011 12:03 PM, Shawn McKenzie wrote: On 03/11/2011 01:28 PM, Danny wrote: Hi guys, I have a form that has a long list of radio-bottons inside of it. The radio-buttons are dynamically created via php and MySQL. Here is an example of one of the radio buttons: input type=radio name=?php print (radio_.$result_from_mysql) ; ? value=0 input type=radio name=?php print (radio_.$result_from_mysql) ; ? value=1 Now, when I submit this form to another page for processing, how would I catch the above radio-button's $_POST name since I do not know the name, only that it starts with radio_ ? Thank You Danny The most common and flexible way to do this sort of thing is to use arrays instead: input type=radio name=radio[?php echo $result_from_mysql; ?] value=0 input type=radio name=radio[?php echo $result_from_mysql; ?] value=1 Then: foreach($_POST['radio'] as $key = $value) { echo radio for $key is $value; } Your example would be good if the OP wanted checkbox'es. But with radio buttons, the whole point (most of the time) is to have the form only allow you to have one of the radio input fields selected at any given time. How you showed it, it would not see the uniqueness of the radio button names, and therefor allow more than one of the radio input fields to be selected at a time. One radio button of the same name selected at a time yes. From the OP's code of two radios, one with value 0 and one with value 1, I assumed these were to be a pair with the same name with only one able to be successful. This extends to arrays as well: input type=radio name=radio[] value=0 input type=radio name=radio[] value=1 input type=radio name=radio[555] value=0 input type=radio name=radio[555] value=1 This works as expected, the same as the code I posted. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: $_POST variable
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. -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-+ | BOX | +-+ think -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php