PHP Forms Handling Remote Variables with Periods in Their Names - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Forms Handling Remote Variables with Periods in Their Names - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, June 4, 2019

PHP Forms Handling Remote Variables with Periods in Their Names

PHP Forms


Handling Remote Variables with Periods in Their Names

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.

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'].

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="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'].


No comments:

Post a Comment

Post Top Ad