Jump to content

[SOLVED] Help with PHP Code


schoolmommy

Recommended Posts

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>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.