Jump to content

Recommended Posts

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

<!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

 

 <?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!

Link to comment
https://forums.phpfreaks.com/topic/182679-processing-form-information/
Share on other sites

part of the problem might be that you're spelling color two different ways. Because from what you've posted that would work.  Except I would do

    <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>

then

$colors=array("red","blue","orange","green","white","yellow","purple","brown","black");  // whitelist the colors for security 
if(isset($_POST['user_color']) && in_array($_POST['user_color'],$colors)) { 
echo "<body style='background-color: ".$_POST['user_color'].";'</body>";
}

 

That works beautifully, thank you.

 

I still a little stuck. If I wanted to control the background-image rather than background-color what would be the correct syntax.

 

I have tried this:

 

//echo "<body style='background-color: ".$_POST['user_color'].";'</body>";
echo "<body style='background-image: url( ".$_POST['user_color'].";.gif)'</body>";

 

but it doesn't work

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.