aidoDel Posted November 23, 2009 Share Posted November 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted November 23, 2009 Share Posted November 23, 2009 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>"; } Quote Link to comment Share on other sites More sharing options...
aidoDel Posted November 23, 2009 Author Share Posted November 23, 2009 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 Quote Link to comment Share on other sites More sharing options...
peterg0123 Posted November 23, 2009 Share Posted November 23, 2009 Hi, Try removing the ";" before ".gif" echo "<body style='background-image: url( ".$_POST['user_color'].".gif)'</body>"; Cheers Peter Quote Link to comment Share on other sites More sharing options...
aidoDel Posted November 23, 2009 Author Share Posted November 23, 2009 brilliant! thank you very much, you both have help ensure I get to bed tonight. 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.