Jump to content

Archived

This topic is now archived and is closed to further replies.

stridox

Switch help?

Recommended Posts

I know that this works

[code]<?
$Function = ( $_POST['f'] ) ? $_POST['f'] : $_GET['f']; //Gets the function
switch( $Function )
{
case 'a':
?>
<a href="switcher.php?f=b">Function B</a><br>
<a href="switcher.php">No Function (Default used)</a>
<?
break;
case 'b':
?>
This is the function 'b'.<br>
<a href="switcher.php?f=a">Function A</a><br>
<a href="switcher.php">No Function (Default used)</a>
<?
break;
default:
?>
<a href="switcher.php?f=a">Function A</a><br>
<a href="switcher.php?f=b">Function B</a><br>
<a href="switcher.php?f=c">Function C (Does not exist, use default)</a>
<?
};
?>
[/code]

But I just installed PHP on this machine, and I'm getting this:

[pre]Notice: Undefined index: f in f:\site\switcher.php on line 5

Notice: Undefined index: f in f:\site\switcher.php on line 5
Function A
Function B
Function C (Does not exist, use default)[/pre]

Any ideas?

Share this post


Link to post
Share on other sites
It's because of the set level of error reporting.

Use isset:
[code]$Function =  isset($_POST['f'])? $_POST['f'] : $_GET['f']; [/code]

Share this post


Link to post
Share on other sites
I dont understand why your using the $_POST array at all.

[code=php:0]
$Function =  isset($_GET['f']) ? $_GET['f'] : '';
[/code]

Share this post


Link to post
Share on other sites
Okay, here's the deal: php can be set to leave or inlcude the reporting of notices, warnings en fatal errors. Likely, on the other machine it was set not to report notices. Mind you, this is NOT AN ERROR, it's a notice. It's triggered when you refer to var (or in this case index) not set.

In your script you only refer to two vars and indexes: $_POST['f'] and $_GET['f'].

Appearantly, in some state of your application, neither $_POST['f'] nor $_GET['f'] are set.

Alternative:
[code]if(isset($_POST['f])) {
$func = $_POST['f];
elseif(isset($_GET['f'])) {
$func = $_GET['f];
else {
$func = '';
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.