YinYangKim Posted March 12, 2014 Share Posted March 12, 2014 <html> <head> <!--title>Hello</title--> <link rel="stylesheet" href="uvcs.css" type="text/css" /> </head> <body> <div id=everything> <div id=header> This is the header </div> <div id=clear></div> <div id=navmenu> <div id=navmenu> <ul> <li> <a href="index.php?cmd=home">HOME</a> </li> <li> <!--submenu--> <a>VIEW</a> <ul> <li> <a href="index.php?cmd=student">STUDENT PROFILE</a> </li> <li> <a href="index.php?cmd=employee">EMPLOYEE</a> <!--subsubmenu--> <!--ul> <li> <a href=#>subsubmenu1</a> <a href=#>subsubmenu2</a> </li> </ul--> </li> </ul> </li> <li> <a href="index.php?cmd=courses">COURSES</a> </li> <li> <a href="index.php?cmd=aboutus">ABOUT UVCS</a> </li> <li> <a href="index.php?cmd=itsupport">IT SUPPORT</a> </li> <li> <a href="index.php?cmd=login">LOGIN</a> </li> </ul> </div> </div> <div id=clear></div> <div id=maincontent> <?php include("selector.php"); ?> </div> <div id=sidebar> side bar </div> <div id=clear></div> <div id=footer> Footer </div> </div> </body> </html> <?php $cmd =$_GET['cmd']; switch($cmd) { case 'home': include_once "home.php"; break; case 'student': include_once "student.php"; break; case 'employee': include_once "employee.php"; break; case 'courses': include_once "courses.php"; break; case 'aboutus': include_once "aboutus.php"; break; case 'itsupport': include_once "itsupport.php"; break; case 'login': include_once "login.php"; break; default: include_once "home.php"; break; } ?> These are my codes that supposedly would show home.php (which contains only the text "you are viewing home.php"), when you open the index.php. But for some reason I would get the line Notice: Undefined index: cmd in C:\xampp\htdocs\UVCSPortal\selector.php on line 2 you are viewing home.php I thought with these codes with would go directly to default case. But instead it reads the <?php include("selector.php"); ?> in index.php twice, I think. I tried using isset, but couldn't understand it. Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/ Share on other sites More sharing options...
Ch0cu3r Posted March 12, 2014 Share Posted March 12, 2014 Check $_GET['cmd'] exists using isset $cmd = isset($_GET['cmd']) ? $_GET['cmd'] : 'home'; // if $_GET['cmd'] does not exist, set $cmd to home by default Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472306 Share on other sites More sharing options...
YinYangKim Posted March 13, 2014 Author Share Posted March 13, 2014 Check $_GET['cmd'] exists using isset $cmd = isset($_GET['cmd']) ? $_GET['cmd'] : 'home'; // if $_GET['cmd'] does not exist, set $cmd to home by default Hi, I did as you told and got home as output after <?php echo $cmd; ?> I'm still wondering why it looks like my cmd gets checked twice. O_O Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472404 Share on other sites More sharing options...
trq Posted March 13, 2014 Share Posted March 13, 2014 I'm still wondering why it looks like my cmd gets checked twice. O_O What? Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472417 Share on other sites More sharing options...
YinYangKim Posted March 13, 2014 Author Share Posted March 13, 2014 What? Notice: Undefined index: cmd in C:\xampp\htdocs\UVCSPortal\selector.php on line 2 you are viewing home.php This is the output when I open index.php. Those two lines. If I click on the link with cmd = home, the first line disappears. Following that logic, on the first load of index.php, it checks the content of cmd twice, printing the first line that way because cmd is null and the 2nd line because it already contains home. Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472459 Share on other sites More sharing options...
trq Posted March 13, 2014 Share Posted March 13, 2014 The error is caused by this line: $cmd =$_GET['cmd']; The 'cmd' does not yet exist and you have done nothing to set a default. This line: default: include_once "home.php"; break; Then causing the output "you are viewing home.php" because $cmd has fallen through all your switch statements and into the default clause. Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472512 Share on other sites More sharing options...
YinYangKim Posted March 14, 2014 Author Share Posted March 14, 2014 The error is caused by this line: $cmd =$_GET['cmd']; The 'cmd' does not yet exist and you have done nothing to set a default. This line: default: include_once "home.php"; break; Then causing the output "you are viewing home.php" because $cmd has fallen through all your switch statements and into the default clause. got a fix for this? Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472602 Share on other sites More sharing options...
Ch0cu3r Posted March 14, 2014 Share Posted March 14, 2014 (edited) Did you not see my post earlier? http://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/?do=findComment&comment=1472306 If you don't want $cmd to have the default value of home, then change 'home' in my code to null Edited March 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472617 Share on other sites More sharing options...
YinYangKim Posted March 15, 2014 Author Share Posted March 15, 2014 Did you not see my post earlier? http://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/?do=findComment&comment=1472306 If you don't want $cmd to have the default value of home, then change 'home' in my code to null First off, I thought it was just used for checking. Second, didn't work. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472655 Share on other sites More sharing options...
afaaro Posted March 15, 2014 Share Posted March 15, 2014 try this one $cmd = (isset($_GET['cmd])) ? $_GET['cmd] : "" ; Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472670 Share on other sites More sharing options...
YinYangKim Posted March 15, 2014 Author Share Posted March 15, 2014 try this one $cmd = (isset($_GET['cmd])) ? $_GET['cmd] : "" ; didn't work. or maybe i used it wrong. i placed it before <?php include("selector.php"); ?> in my index.php Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472671 Share on other sites More sharing options...
afaaro Posted March 15, 2014 Share Posted March 15, 2014 if (isset($_GET['cmd'])) { $cmd = $_GET['cmd']; } else { $cmd = ""; } Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472674 Share on other sites More sharing options...
Solution Ch0cu3r Posted March 15, 2014 Solution Share Posted March 15, 2014 didn't work. or maybe i used it wrong. i placed it before <?php include("selector.php"); ?> in my index.php My code is to replace this line $cmd =$_GET['cmd']; in selector.php, so the first three lines should read like this in selector.php <?php $cmd = isset($_GET['cmd']) ? $_GET['cmd'] : 'home'; switch($cmd) Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472687 Share on other sites More sharing options...
YinYangKim Posted March 15, 2014 Author Share Posted March 15, 2014 (edited) My code is to replace this line $cmd =$_GET['cmd']; in selector.php, so the first three lines should read like this in selector.php <?php $cmd = isset($_GET['cmd']) ? $_GET['cmd'] : 'home'; switch($cmd) It worked! Thanks a lot. Although I still don't get why this happened, considering I've used this code on other projects. Oh well. Edited March 15, 2014 by YinYangKim Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472695 Share on other sites More sharing options...
Ch0cu3r Posted March 15, 2014 Share Posted March 15, 2014 (edited) You other project most probably didn't have display_errors enabled, or error_reporting was set to ignore notices. When developing a script you should make sure error_reporting is set to E_ALL and display_errors is enabled. Edited March 15, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286915-undefined-index-cmd-using-switch-case/#findComment-1472707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.