2012-09-30 29 views
0

我正在尝试创建一个页面,输入的温度高于70°时,结果页面将变为红色,并且如果温度低于70°,那么它会变成蓝色。我已经有了提交页面代码第一部分的示例页面。我只是不想让文字变成颜色,我希望整个页面根据温度变换颜色。谁能帮忙?如何根据输入的值更改Javascript中的背景颜色

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

回答

1

我想一个简单的<body>样式将在这里工作?

<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); ?>"> 
0

使用这样

<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 
?> 
+0

它没有工作。 – getlaura

+0

什么颜色来? –

+0

@getlaura我编辑了代码,现在检查它会工作。 –

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

我不认为JavaScript是正确的方式去做这件事。 – Blender

+0

是的,但看到问题标题 – Afshin

+1

我看不出为什么你需要使用PHP来输出修改HTML元素的CSS的JavaScript ... – Blender

相关问题