PHP Question
Author
Discussion

thunderstruck

Original Poster:

8,883 posts

312 months

Thursday 3rd November 2005
quotequote all
I have a page - call it index.php

The first visit to the page, the URL is /website/index.php

There is a link on index.php, which links to index.php passing a parameter, p.

That is, it links to /website/index.php?p=1 for example.

Now in the code of index.php, I do this:

if ($_REQUEST['p'])
{
....

This is fine when the parameter is on the URL, but gives an unknown index error when just visiting index.php.

How does one go about reloading a page and check whether something has been submitted for example without getting this error?

Cheers!

_dobbo_

14,619 posts

278 months

Thursday 3rd November 2005
quotequote all
ok, php isn't my thing, but wouldn't you just use the "else" statement to catch anything that doesn't match your if statement?

Or failing that, (and I'm guessing the syntax for php)

elseif ($_REQUEST[''])
{

thunderstruck

Original Poster:

8,883 posts

312 months

Thursday 3rd November 2005
quotequote all
_dobbo_ said:
ok, php isn't my thing, but wouldn't you just use the "else" statement to catch anything that doesn't match your if statement?

Or failing that, (and I'm guessing the syntax for php)

elseif ($_REQUEST[''])
{


Doesn't work like that unfortunately.

If I have:

if ($_REQUEST['p'])
{
Do something;
}
else
{
Do something else;
}

...it still throws an error about "Unknown index p" if the page is loaded without the parameter.

_dobbo_

14,619 posts

278 months

Thursday 3rd November 2005
quotequote all
Ok, how about (and again I don't know the syntax so I'll just use words);

if $_REQUEST not equal to 'p'
{
do stuff
}
else
{
do other stuff
}

docevi1

10,430 posts

278 months

Thursday 3rd November 2005
quotequote all
if (isSet( $_GET['p'] ) )
{

}
else
{

}

Get is used to pull parameters from the url string array (i.e. index.php?p=george), _POST is used to pull from the form post array. isset simply checks to see if something is equal to p and returns a boolean (true / false).

REmember you can use if statements with html:
<?PHP
if(...)
?>
some html
<?
}
else
{
?>
some more html.
<?
}

>> Edited by docevi1 on Thursday 3rd November 20:16

size13

2,033 posts

287 months

Thursday 3rd November 2005
quotequote all
Like D says, use $_POST

thunderstruck

Original Poster:

8,883 posts

312 months

Friday 4th November 2005
quotequote all
docevi1 said:
if (isSet( $_GET['p'] ) )
{

}
else
{

}

Get is used to pull parameters from the url string array (i.e. index.php?p=george), _POST is used to pull from the form post array. isset simply checks to see if something is equal to p and returns a boolean (true / false).

REmember you can use if statements with html:
<?PHP
if(...)
?>
some html
<?
}
else
{
?>
some more html.
<?
}

>> Edited by docevi1 on Thursday 3rd November 20:16


Brilliant, I will try that thanks.

I'm always confused between GET, POST and REQUEST.

So:

GET: Get parameters from URL
POST: Get form values from form submission
REQUEST: ?

size13

2,033 posts

287 months

Friday 4th November 2005
quotequote all
thunderstruck said:

REQUEST: ?
Never use it!

Plotloss

67,280 posts

300 months

Friday 4th November 2005
quotequote all
Yep REQUEST is the old way as far as I can make out...