1

I have an html form (method = "get", action = "cart.php") in which I use two buttons to send the same form.

echo '<input type="image" src="data/images/back.png" height="40px" id="back" name="back">';
echo '<input type="image" src="data/images/continue.png" height="40px" id="go" name="go">';

now, on the PHP side, I'd like to check which one of the button was clicked (the "go" button or the "back" button).

I've done:

if(isset($_GET['go.x']) == true)
{
   echo 'form submitted using button GO';
}
else
{
   echo 'form submitted using button BACK';
}

I use the 'go.x' variable name as the form is passing an "go.x" and "go.y" variables (the exact x,y positions inside the image button where the user clicked)

However, the isset() function always return false... how it could be? thanks

4
  • What happens when you print_r($_GET);? Commented Apr 19, 2014 at 13:43
  • x & y are separate GET values, just use 'go'. Commented Apr 19, 2014 at 13:43
  • oh my god... I solved on my own... I did isset($_GET[back_x] == true) and IT WORKED Commented Apr 19, 2014 at 13:43
  • @Chirs: this doesn't work. already tried it. However I solved Commented Apr 19, 2014 at 13:44

3 Answers 3

3

From the manual:

Because foo.x and foo.y would make invalid variable names in PHP, they are automagically converted to foo_x and foo_y. That is, the periods are replaced with underscores. So, you'd access these variables like any other described within the section on retrieving variables from external sources. For example, $_GET['foo_x'].

There's an open issue raised against this replacement of periods with underscores in PHP that suggests that the behaviour was intended to translate submitted variable names into valid global names when register_globals is turned on.

Now that the deprecated register_globals is finally being removed, it looks like there's a chance the behaviour will be changed to the one you were expecting all along.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks. However I'm not completely satisfied by your answer: foo.x and foo.y would not be variables in any case, they are just string keys of the associative array $_GET. PHP doesn't forbid strings with periods inside them so why this odd behavior?
My guess would be for historical reasons related register_globals which, if set, would make the individual variables accessible directly, too. That is, if you had register_globals turned on, then as well as $_GET['foo.x'] you'd expect $foo.x to be created, which it couldn't be, as it's not a valid name. There is an open bug raised against this behaviour that seems to indicate that it's for that reason and that it might change now register_globals is going.
Interesting. From the open bug report it seems that's the intended behavior. They even posted the code that perform the period->underscore conversion.
Yeah. The code's been in there for at least fifteen years; if you want to figure out exactly why it was put there you might need an archaeologist rather than a programmer. From what I can tell from this unofficial history it might well have been written before $_GET existed.
mmm ok. Now I figure out the whole problem. In any case I've always felt that PHP is a very screwed up programming language from the standardization point of view (much like HTML&CSS), albeit being one of my favorite languages :-). For example, I don't like the forced C style paradigm which in this case fits in very badly.
1

I'm answering my own question as I tried:

if(isset($_GET[go_x] == true))
{
}

and it worked. Now my new question is: Why in the world PHP translates my go.x GET variable into go_x???

3 Comments

is it an apache webserver issue?
It will do that with all variables passed to a PHP script: php.net/manual/en/…
It's a legacy thing. PHP used to automatically import values from GET, POST, and cookie values directly into the global scope as named variables (e.g., $go_x). Those can't have periods, so it translates them to underscores.
0

You elaborate in javascript the object with name "go", but php doesn't understand the extrapolation of variable x ("go.x"), I suggest to elaborate before the data in javascript and than set input types "hidden" to sent data to the php page, for futher elaborations.

ps.

if(isset($_GET['go.x']) == true)
It's equal to say
if(isset($_GET['go.x']))

(https://www.php.net/isset)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.