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.

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

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'];
}
if(isset($_GET['submit']) && isset($_GET['test']) {
$y=$_GET['test'];
}

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'];
}

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

commented: Thnx for pointing this out +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.