Jump to content

I don't understand how to change color for each text/background color?


Recommended Posts

For this assignment, you are to create one set of PHP pages. The set will contain a page with a form, and a page that processes the data in this form. In this part you will create a set of pages that could be used to test different text and background color combinations. Your first page should contain a form with two selection lists: one for the background color, and one for the text color. Your second page should process this form, displaying some sample text in the desired color with the desired background color. In addition, your program should display an error message (in black text on a white background) if the user selects the same color for both text and background. The specific colors you have in your selection lists are up to you, but you should have at least five colors in each list.

 

http://hills.ccsf.edu/~schow11/cs130a/hw3test.html

Notice: Undefined index: textcolor in /students/schow11/public_html/cs130a/hw3.php on line 24

 

someone told me to use this format 

 

<style type='text/css'> 

.... body { background-color: red; } 
.... </style

 

Then use class='bgcolor' in the opening tag of any element you want to apply that to 

<span class='bgcolor'> .... to just color some words in a paragraph 

You don't show any other code, but can do something like this using the $_POST value or a default color if not set

<?php
if(isset($_POST['background']) && trim($_POST['background']) !=''){
    $background = $_POST['background'];
} else {
    $background = "#FFFFFF";
}
?>

<style type='text/css'> 
body {
    background-color: <?php echo $background;?>;
 } 
</style>
Thank for your help. I had it working except it won't print the text color's color but instead it printed out the bg color on both statememts

Now since I was figuring things out, I caused a error that I don't get. '

 

Notice: Undefined variable: textcolor in /students/schow11/public_html/cs130a/hw3.php on line 57

 


 

<?php

 

if (isset($_POST['submit'])) {

    //check each if each post is set and it's value is data expect

    }

   

if(isset($_POST['background']) && trim($_POST['background']) !=''){

    $background = $_POST['background'];

} else {

    $background = "#FFFFFF";

}

 

function pmt($textcolor, $bgcolor){

}

        

        if (isset($_POST['red'])) {

        $textcolor = $_POST['red'];

    }

    if (isset($_POST['green'])) {

        $textcolor = $_POST['green'];

    }

    if (isset($_POST['blue'])) {

        $textcolor = $_POST['blue'];

    }

    if (isset($_POST['orange'])) {

        $textcolor = $_POST['orange'];

    }

    if (isset($_POST['yellow'])) {

        $textcolor = $_POST['yellow'];

    }

 

   

        if (isset($_POST['red'])) {

        $bgcolor = $_POST['red'];

    }

    if (isset($_POST['green'])) {

        $bgcolor = $_POST['green'];

    }

    if (isset($_POST['blue'])) {

        $bgcolor = $_POST['blue'];

    }

    if (isset($_POST['orange'])) {

        $bgcolor = $_POST['orange'];

    }

    if (isset($_POST['yellow'])) {

        $bgcolor = $_POST['yellow'];

    }

        

        

        print "<p>The text color you chose is $textcolor color";

        print "<p>The background color you chose is $bgcolor color";

        }

        ?> 

My code did not match your form names and was an example.

 

Before using any variables they must exist.

 

Your function is looking for $textcolor and $bgcolor which doesn't exist in your code except inside the function.

Then you never called on the function.

 

You were way off, take a look at what this does, you need to learn why and how or this homework is useless.

<?php
//define variables
$textcolor = "black";
$bgcolor   = "white";

//check if form submitted
if (isset($_POST['submit'])) {
    
    //if below statements are true will overwrite the variable
    
    //check if textcolor is set
    if (isset($_POST['textcolor'])) {
        $textcolor = $_POST['textcolor'];
    }
    
    //check if bgcolor is set
    if (isset($_POST['bgcolor'])) {
        $bgcolor = $_POST['bgcolor'];
    }
    
}
?>  


<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Homework 3</title>
        
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css">
        <link rel="stylesheet" href="css/ui.totop.css">
        <link rel="stylesheet" href="css/style.css">
<style type='text/css'>
body {
    color: <?php echo $textcolor;?>;
    background-color: <?php echo $bgcolor;?>;
 }
</style>    
    </head>
    <body>
        <div class="container">
        
        <form method="post" action="" >
        <h2>Conditional Statements - Homework 3</h2>
    
        <p> Please choose a color for the text</p>
        <select name="textcolor">
            <option value="black">Black</option>
            <option value="white">White</option>
            <option value="red">Red</option>
            <option value="green">Green</option>
            <option value="blue">Blue</option>
            <option value="orange">Orange</option>
            <option value="yellow">Yellow</option>
        </select>
       <br>
       <br>
        <p> Please choose a color for the background</p>
            <input type="radio" name="bgcolor" value="black" />Black<br />
            <input type="radio" name="bgcolor" value="white" />White<br />
            <input type="radio" name="bgcolor" value="red" />Red<br />
            <input type="radio" name="bgcolor" value="green" />Green<br />
            <input type="radio" name="bgcolor" value="blue" />Blue<br />
            <input type="radio" name="bgcolor" value="orange" />Orange<br />
            <input type="radio" name="bgcolor" value="yellow" />Yellow<br />
            
        <p><input type="submit" name="submit" value="Submit"></p>
        </form>
        
        <p>The text color is <?php echo $textcolor;?></p>
        <p>The background color is <?php echo $bgcolor;?></p>
        </div>
    </body>
</html>
Edited by QuickOldCar

i copied and paste your code to see if it will work but im getting this error

Notice: Undefined variable: textcolor in /students/schow11/public_html/cs130a/hw3.php on line 57''

 

http://hills.ccsf.edu/~schow11/cs130a/hw3.html

You didn't copy paste it.

 

Mine had:

<form method="post" action="" >

 

You linked back to your other code

 

<form method="post" action="hw3.php" >

 

Do this with a .php extension as well and not .html, if you put all the code at hw3.php will work.

Edited by QuickOldCar
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.