schoolmommy Posted April 19, 2007 Share Posted April 19, 2007 Hi! I'm pretty new to PHP and am having a problem with a program I have to write. When run, the screen usually displays the html but not the php output. Can someone take a look at my code and see if they can see the probem. Thanks! ******************** PHP Code ******************** <?PHP //Get data extract $_POST; // Determine if data is blank if ($_POST['color']){ header("Location: index.html"); exit; } // Determine the color & index number switch($_POST['color']) { case 'Red': $resultIndex = 0; $resultColor = "Red"; break; case 'Blue': $resultIndex = 1; $resultColor = "Blue"; break; case 'Green': $resultIndex = 2; $resultColor = "Green"; break; } ?> <html> <head> <title>Pick A Color</title> </head> <body> <p>The index is: <? echo "$resultIndex"; ?></p> <p>The color is: <? echo "$resultColor"; ?></p> </body> </html> ****************** html code ****************** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Pick A Color</title> </head> <body> <form method="POST" action="PickAColor.php"> <input type="Radio" name="color" value="Red"> Red<br> <input type="Radio" name="color" value="Blue"> Blue<br> <input type="Radio" name="color" value="Green"> Green<br> <p> <input type="submit" name="submit1" value="Pick A Color"> </p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 First, you don't need the line <?php extract $_POST; ?> which is formatted incorrectly. Delete it. Next, change <?php if ($_POST['color']){ ?> to <?php if (!isset($_POST['color']) { ?> Ken Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted April 19, 2007 Share Posted April 19, 2007 the isset() function checks if a variable is set or not, and the "!" reverses the answer(im not sure why you would want to re-direct if the value is NOT set. I would presume you would only want to if it WAS set. So maybe take that out). so if its Not set it will go to the index page. The next thing you may wish to do is add a default to your case statement so you don't get unset variables... <?PHP // Determine if data is blank if (!isset($_POST['color'])){ header("Location: index.html"); exit; }else{ // Determine the color & index number switch(@$_POST['color']) { case 'Red': $resultIndex = 0; $resultColor = "Red"; break; case 'Blue': $resultIndex = 1; $resultColor = "Blue"; break; case 'Green': $resultIndex = 2; $resultColor = "Green"; break; default: $resultIndex = 3; $resultColor = "Not Set!"; break ; } } ?> I have also added the "@" symbol to the start of the switch statement, this surpresses errors if it isnt set. Thanks, Nathan Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 The use of the "@" symbol should be discouraged, especially during development of code. It's much better to fix the problems than to hide them. Ken Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted April 19, 2007 Share Posted April 19, 2007 I would tend to agree, But what is he trying to achieve? If the $_POST['color'] variable is NOT set, then why redirect? Would you want to redirect if it was set? and pass it on? And is he testing for blank? or Unset? -- Nathan Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 Blank or unset I'd imagine, but the only reason I see for using "@" to suppress errors is if you are going to handle them some other way, but this doesn't appear to be the case in that script. Quote Link to comment Share on other sites More sharing options...
schoolmommy Posted April 19, 2007 Author Share Posted April 19, 2007 Hi! I made the changes and ran it and got the following error: ******************************************* The page cannot be displayed The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access. -------------------------------------------------------------------------------- Please try the following: Contact the Web site administrator if you believe that this request should be allowed. Make sure that the Web site address displayed in the address bar of your browser is spelled and formatted correctly. HTTP Error 405 - The HTTP verb used to access this page is not allowed. Internet Information Services (IIS) -------------------------------------------------------------------------------- Technical Information (for support personnel) Go to Microsoft Product Support Services and perform a title search for the words HTTP and 405. Open IIS Help, which is accessible in IIS Manager (inetmgr), and search for topics titled Setting Application Mappings, Securing Your Site with Web Site Permissions, and About Custom Error Messages. *************************************************************************** I have run html/php files before and haven't had any problems. Could it be something has changed in my IIS? Thanks! Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted April 19, 2007 Share Posted April 19, 2007 None of those errors are PHP errors, I would check with your hosting service. -- Nathan Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 Either you are using some very weird browser to test it on that doesn't send the proper GET request, or, your IIS server is now unaware of how to handle it. Quote Link to comment Share on other sites More sharing options...
schoolmommy Posted April 19, 2007 Author Share Posted April 19, 2007 I had PHP installed on our system under IIS, but my husband had reformatted our hardrive and the PHP was deleted. I have reinstalled it and it says it's working, but when I run the program I'm still getting that the page can't be displayed. I'm using IE. I'm going to try running it on my laptop to see if it's doing the same thing. It's also setup to run php. Thanks! Quote Link to comment Share on other sites More sharing options...
schoolmommy Posted April 19, 2007 Author Share Posted April 19, 2007 I just tested the program on my laptop. When I choose a color and hit the submit button, nothing happens. I tested another php file that I had done and it worked just fine. Thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 Please post the code you're testing. Ken Quote Link to comment Share on other sites More sharing options...
schoolmommy Posted April 19, 2007 Author Share Posted April 19, 2007 I did some checking on the main machine I was using and found that some stuff had been turned off that was keeping my code from working. After making those changes, the program works fine on this machine now I found some things on my laptop were not set right either and made the changes I thought would fix the problem, but not quite there yet. The IIS looks right, but the problem I had with the main computer was that another file under the default directory had some defaults turned off. My laptop doesn't have that same directory, so I need to see if there is something else causing it to not work. I've tested a php file this morning on the laptop and it worked, but not this program. The code is below: <?PHP // Determine if data is blank if (!isset($_POST['color'])){ header("Location: index.html"); exit; }else{ // Determine the color & index number switch(@$_POST['color']) { case 'Red': $resultIndex = 0; $resultColor = "Red"; break; case 'Blue': $resultIndex = 1; $resultColor = "Blue"; break; case 'Green': $resultIndex = 2; $resultColor = "Green"; break; default: $resultIndex = 3; $resultColor = "Not Set!"; break ; } } ?> <html> <head> <title>Lab3Prob4</title> </head> <body> <p>The index is: <? echo "$resultIndex"; ?></p> <p>The color is: <? echo "$resultColor"; ?></p> </body> </html> ********************** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Lab 3 Problem 4</title> </head> <body> <form method="POST" action="PickAColor.php"> <input type="Radio" name="color" value="Red"> Red<br> <input type="Radio" name="color" value="Blue"> Blue<br> <input type="Radio" name="color" value="Green"> Green<br> <p> <input type="submit" name="submit1" value="Pick A Color"> </p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted April 19, 2007 Share Posted April 19, 2007 Is there an error message that we can work from? -- Nathan Quote Link to comment 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.