Jump to content

how do i do error handling for this php code?


xaznbabyx

Recommended Posts

i got it to change colors now I have to create an error and display the error message if they choose a color like black text and blue background etc. How would i create the error handling?

 

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

 

Part I

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.

 
Link to comment
Share on other sites

Use the comparison operator to see if the values are the same, Simple if/else logic

// When you get the background and text color from the user 
// check if the chosen background and text colors are the same
if($background_color == $text_color)
{
    // colors are the same so output error message saying colors must not be the same
    // set page background to be white and text color to black
}
// colors are not the same
else
{
   // so set the page background and text color to the chosen values.
}

Link to comment
Share on other sites

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

i did somethign like this but the error isn't displaying.. also how do u place in the php format with the indentations?

 

 

<?php

 

//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'];

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

    }

    

    //check if bgcolor is set

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

        $bgcolor = $_POST['bgcolor'];

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

 

    } else {

    if($background_color == $text_color)

        print '<p class="error">Please do not choose the text and background color!"</p>';

       

}

    

}

?>

Edited by xaznbabyx
Link to comment
Share on other sites

Your if statement for comparing the colors should not be in an else statement. It must be on its own. Next you need to use the correct variable names, $background_color should be $bgcolor and $text_color should be $textcolor

 

When you echo the error message you should be resetting $bgcolor and $textcolor back to their default values, which is black and white respectively. 

<?php
 
//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'];
        print "<p>The text color you chose is $textcolor color</p>";
    }
    
    //check if bgcolor is set
    if (isset($_POST['bgcolor'])) {
        $bgcolor = $_POST['bgcolor'];
        print "<p>The background color you chose is $bgcolor color";
 
    }

    if($bgcolor == $textcolor)
    {
        print '<p class="error">Text and background color cannot be the same!"</p>';
        // reset colors to default values
        $bgcolor = 'white';
        $textcolor = 'black';  
    }
}
?> 

 

how do u place in the php format with the indentations?

When posting either wrap the code inside  


tags or click the <> button in the editor

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.