0

Hi
I am very new php propgramming. But I have done classic asp.
I have a string request, eg www.xyz.com?test=1

In my php page, I would like to read test value.
On my index.php, I have this code.

<?
 $y=$_GET["test"];
?>

But I am getting error, Undefined index: test in index.php.
I can see, when I call index page first time, I call just as www.xyz.com.

Please can some help.

Many thanks.

Edited by diafol: use code tags

4
Contributors
5
Replies
9
Views
4 Years
Discussion Span
Last Post by Bob Hensley
0

Hi
I am very new php propgramming. But I have done classic asp.
I have a string request, eg www.xyz.com?test=1

In my php page, I would like to read test value.
On my index.php, I have this code.
<?
$y=$_GET["test"];
?>

But I am getting error, Undefined index: test in index.php.
I can see, when I call index page first time, I call just as www.xyz.com.

Please can some help.

Many thanks.

You need to check for the existence of the query string prior to using it. When calling http://domain.tld/index.php there is no query string. Thus $_GET['test'] does not exist. Hence the notice you're receiving.

if (isset ($_GET['test']))
  echo $_GET['test'];

Edited by Bob Hensley

0

Form values get into the $_GET array after submitting the form. So you first check for the value if it exists:

if(isset($_GET['test']) {

    $y=$_GET['test'];
}

or you might want to check whether the form has been submitted:

if(isset($_GET['submit']) && isset($_GET['test']) {

    $y=$_GET['test'];
}
0

In the last two posts above there are bracets missing. So the correct code is:

if(isset($_GET['submit']) && isset($_GET['test'])) {

    $y=$_GET['test'];
}
1

In the last two posts above there are bracets missing. So the correct code is:

Mltiple isset () calls are not needed. That's an unnecessary action due to the isset () function being capable of receiving multiple parameters. Reference PHP's documentation on the isset () function.

When multiple parameters are passed to isset () it will return true if all are set or false if any of them are not. Thus it's the same logic as the code you've presented, but removes unnecessary code.

isset ($_GET['submit'], $_GET['test'])

Edited by Bob Hensley

Votes + Comments
Thnx for pointing this out
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.