Gianluca Ghettini
Gianluca Ghettini

Reputation: 11628

PHP: isset() doesn't detect GET variable

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

Upvotes: 1

Views: 266

Answers (3)

Matt Gibson
Matt Gibson

Reputation: 38238

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.

Upvotes: 3

FabioSorre
FabioSorre

Reputation: 310

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)

Upvotes: 0

Gianluca Ghettini
Gianluca Ghettini

Reputation: 11628

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???

Upvotes: 1

Related Questions