0

I am trying to create a page that, upon entering a temperature of or higher than 70, the result page will turn red, and if the temperature is below 70, then it will turn blue. I already have a sample page for the first part of the code for the submit page. I just don't want the words to turn colors, I want the entire page to turn colors based on the temperature. Can anyone help?

<html>
<body>
<?php
if (isset($_POST['temperature'])): //isset determines if var has valid contents, even empty string

    //if var is set, show what it contains
    $myFahrenheit = (int)$_POST['temperature']; //cast value sent via post to an integer

    $myCelsius = intval(($myFahrenheit-32) * (5/9)); //calc celsius
    echo '<h1><font color="blue">You entered ' . $myFahrenheit . ' in Fahrenheit!!</font><br />';
    echo '<h1><font color="red"> It is '. $myCelsius .' in celsius!!</font><br />';
    //put link on page to reset form
    print '<a href="' . $_SERVER['PHP_SELF'] . '">Reset page</a>';
else:
    //show form
?>
    <!--Note the server variable indicating the page we are on -->
    <form action="<? print $_SERVER['PHP_SELF']; ?>" method="post">
    Enter your temperature in Fahrenheit <input type="text" name="temperature"><br>
    <input type="submit" value="Show me the temperature in celsius!!">
    </form>
<?
endif;
?>
</body>
</html>
0

3 Answers 3

1

I guess a simple <body> styling will work here?

<html>
<?php
    $style = '';
    if (isset($_POST['temperature']))
    {
        if ($_POST['temperature'] >= 70)
            $style = 'background-color: red;';
        else
            $style = 'background-color: blue;';
    }
?>
<body style="<?php echo htmlspecialchars($style); ?>">
Sign up to request clarification or add additional context in comments.

Comments

0
if($myFahrenheit > 70)
  echo '<script>document.body.style.background = "red";</script>';
else
  echo '<script>document.body.style.background = "blue";</script>';

3 Comments

I can't see why you would need to use PHP to output JavaScript that modifies the CSS of HTML elements...
Great! That worked! I'm in a class and still learning. Thank you for your help!
Wait, when did the question become about JavaScript? The OP was edited? I guess my quick answer got invalidated.
0

use it like this

<html>
<?php
if (isset($_POST['temperature'])) //isset determines if var has valid contents, even empty string
{//if var is set, show what it contains
$myFahrenheit = (int)$_POST['temperature']; //cast value sent via post to an integer
if($myFahrenheit > 70)
   echo "<body style='background-color:red;'>";
else
   echo "<body style='background-color:blue;'>";
$myCelsius = intval(($myFahrenheit-32) * (5/9)); //calc celsius
 echo '<h1><font color="blue">You entered ' . $myFahrenheit . ' in Fahrenheit!!    </font><br />';
echo '<h1><font color="red"> It is '. $myCelsius .' in celsius!!</font><br />';
//put link on page to reset form
print '<a href="' . $_SERVER['PHP_SELF'] . '">Reset page</a>';
}else{ //show form
?>

1 Comment

@getlaura i have edited the code , check it now it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.