Page 1 of 1

Processing Form information

Posted: Mon Nov 23, 2009 11:38 am
by aidoDel
Hi all, I'm a complete newbie to PHP and I am having some problems. I need PHP to generate content based on a users input to a HTML form.

My first task is to change the background of the page based on the user's favorite color. I can get only one color to work, I don't understand the logic to make each choice represent a different color.

Here is what I have:

HTML PAGE

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>Process Color</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h2 align="center">Pick A Color</h2>
<div id="form-area">
 
<form action="process_color.php" method="post">
    <fieldset>
    <legend>Your Favorite Color is?</legend>
                
                
    <p>
                <label for="color">Please Select One:<br />
                    <select name="user_color" [ ] id="user_color" class="reqd">
                            <option value="" selected="selected">Choose a colour</option>
                            <option value="Red">Red</option>
                            <option value="Blue">Blue</option>
                            <option value="Orange">Orange</option>
                            <option value="Green">Green</option>
                            <option value="White">White</option>
                            <option value="Yellow">Yellow</option>
                            <option value="Purple">Purple</option>
                            <option value="Brown">Brown</option>
                            <option value="Black">Black</option>
                    </select>
                </label>
        </p>
    <p>
            <input name="submit" type="submit" />
            <input name="reset" type="reset" />
        </p>
    
    </fieldset>
</form>
</div>
</body>
</html>

PHP PAGE

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>Processed Form</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h2>Processed Form from the Form Page</h2>
 
<?php
 
    $user_color = $_POST['user_color'];
?>
    
<?php
        if ($user_colour = "Red") {
        echo '<body style="background-color: red;"</body>';
        }
        if ($user_colour = "Blue") {
        echo '<body style="background-color: blue;"</body>';
        }
        if ($user_colour = "Orange") {
        echo '<body style="background-color: orange;"</body>';
        }
        if ($user_colour = "Green") {
        echo '<body style="background-color: green;"</body>';
        }
        if ($user_colour = "White") {
        echo '<body style="background-color: white;"</body>';
        }
        if ($user_colour = "Yellow") {
        echo '<body style="background-color: yellow;"</body>';
        }
        if ($user_colour = "Purple") {
        echo '<body style="background-color: purple;"</body>';
        }
        if ($user_colour = "Brown") {
        echo '<body style="background-color: brown;"</body>';
        }
        if ($user_colour = "Black") {
        echo '<body style="background-color: black;"</body>';
        }
?>
 
Any help would be greatly appreciated!

Re: Processing Form information

Posted: Mon Nov 23, 2009 11:55 am
by Cirdan
In your if statements, you need two equal signs: ==. Two equal signs are comparative, one equal sign is assignment.

I would also suggest looking into the "switch" statement
http://php.net/manual/en/control-structures.switch.php

Re: Processing Form information

Posted: Mon Nov 23, 2009 12:06 pm
by aidoDel
Thanks for your reply.

Changing the code to:

Code: Select all

if ($user_colour == "Red") {
       echo '<body style="background-color: red;"</body>';
}
Results in the background css not being returned.

I have also tried switch as shown below, but again nothing seems to be returned.

Code: Select all

<?php
switch ($user_colour) {
    case "Red":
        echo '<body style="background-color: red;"</body>';
        break;
    case "Blue":
        echo '<body style="background-color: blue;"</body>';
        break;
    case "Orange":
        echo '<body style="background-color: orange;"</body>';
        break;
}
?>

Re: Processing Form information

Posted: Mon Nov 23, 2009 2:23 pm
by Cirdan
Oh...your missing a bracket in your HTML right after the style=""

echo '<body style="background-color: red;"></body>';

Re: Processing Form information

Posted: Mon Nov 23, 2009 3:32 pm
by Jonah Bron
No need for the switch statement. Just use this select input:

Code: Select all

<select name="user_color" [ ] id="user_color" class="reqd">
<option value="" selected="selected">Choose a colour</option>
<option value="#FF0000">Red</option>
<option value="#0000FF">Blue</option>
<option value="#FF8800">Orange</option>
<option value="#00FF00">Green</option>
<option value="#FFFFFF">White</option>
<option value="#FFFF00">Yellow</option>
<option value="#CC0044">Purple</option>
<option value="#AA6600">Brown</option>
<option value="#000000">Black</option>
</select>
And just use this in the target page:

Code: Select all

<body style="background-color:<?php echo $user_colour; ?>">