views:

88

answers:

5

Goal: Trying to get a rollover button state to remain "ON" when URL equals "enhanced.php".

Problem: Button state does not remain "on" when URL equals "enhanced.php".

(Button acts correctly as a rollover)


sidemenu.php sidemenu.php is used as a PHP include on all pages (I don't know if that makes a difference

<?php

$script = $_SERVER['SCRIPT_NAME'];


//Set the default state to OFF and only turn ON if we are on the current URL.

$enhancedstate = OFF;


$pos = strpos($script, "enhanced.php");
if($pos === true) {
$enhancedstate = ON;
}


?>


 <div class="sideMenu">   

   <a href="enhanced.php" 
      onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image1','','/images/Button_ON_01.gif',1)">
      <img src="/images/Button_<? echo $enhancedstate; ?>_01.gif" name="Image1" border="0">
   </a>

Anyone see any reason why the button state does not stay "ON" when the current URL is "enhanced.php". TIA

+11  A: 

strpos returns a int on success, and FALSE on failure.

Change if($pos === true) to if($pos !== false).

The === operator compares values and types. So, on success strpos returns an int, which may have the same value as TRUE, but it's not the same type.

EDIT: As others have said, you should change:

$enhancedstate = OFF to $enhancedstate = 'OFF'

PHP is very forgiving, and will let you use un-quoted strings, but you should try not to, unless ON and OFF are actually constants.

Rocket
Points for being the fastest in a slew of correct answers ;)
Wrikken
I blame my flaky connection, +1
BoltClock
Best site ever. TY from a noob. :-)
lashrew
You're welcome. :-D
Rocket
+1  A: 

strpos() never returns bool(true) as a value. It's either bool(false) or an int.

So, by doing the following:

if($pos === true)

You are basically looking for a bool(true) value which will never happen (=== checks for type and value).

Change it to:

if($pos !== false)

Do not use $pos == true as this will fail if the string starts with enhanced.php.

Andrew Moore
A: 

You need quotes around your strings. Without them, "ON" is treated as a constant.

$enhancedstate = 'ON';
JoJo
They could be constants defined using `define` (e.g. `define("ON", "ON")`, in which case it would be fine to refer to `ON` instead of `"ON"`.
Richard Fearn
A: 

$enhancedstate = stripos($_SERVER['PHP_SELF'], 'enhanced.php') !== false ? 'ON' : 'OFF';

ive never used script_name, but try this one and see if it works.

Ascherer
i changed it :p
Ascherer
A: 

Use

if ($pos !== false) {

as the test instead. See example #2 (Using !==) in the manual page for strpos.

Richard Fearn