Recipe 9.16. Handling Remote Variables with Periods in Their Names


9.16.1. Problem

You want to process a variable with a period in its name, but when a form is submitted, you can't find the variable in $_GET or $_POST.

9.16.2. Solution

Replace the period in the variable's name with an underscore. For example, if you have a form input element named hot.dog, you access it inside PHP as the variable $_GET['hot_dog'] or $_POST['hot_dog'].

9.16.3. Discussion

During PHP's pimply adolescence when register_globals was on by default, a form variable named hot.dog couldn't become $hot.dog'periods aren't allowed in variable names. To work around that, the . was changed to _. While $_GET['hot.dog'] and $_POST['hot.dog'] don't have this problem, the translation still happens for legacy and consistency reasons, no matter your register_globals setting.

You usually run into this translation when there's an element of type image in a form that's used to submit the form. For example, a form element such as <input type="image" name="locations" src="/books/3/131/1/html/2/locations.gif">, when clicked, submits the form. The x and y coordinates of the click are submitted as locations.x and locations.y. So in PHP, to find where a user clicked, you need to check $_POST['locations_x'] and $_POST['locations_y'].

9.16.4. See Also

Documentation on variables from outside PHP at http://www.php.net/language.variables.external.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net