根据用户选择使用PHP,CSS和HTML更改文本颜色

时间:2015-08-24 17:54:34

标签: php html css

我的作业要求我制作一个颜色选择列表,然后使用PHP以所选颜色打印一个语句。这正是它所说的:

  1. 使用带有以下颜色选项的选择选项列表创建HTML表单: 红色,蓝色和绿色。

    当用户做出选择并单击提交按钮显示时 跟随他们选择的任何颜色的线。如果他们选择红色,例如, 它看起来像......

    让我高兴

  2. 使用CSS类设置文本的颜色。

    这就是他所说的:

    HTML

        <p>
        <select name="colorchoice" size="1">
        <option value="redtext">Red</option>
        <option value="bluetext">Blue</option>
        <option value="greentext">Green</option>
        </select>
        </p>
        <p>
        <input type="submit" value="Submit Information">
        </p>
    

    PHP

        <?php
        $colorvalue = $_POST['colorchoice'];
        print "<span class='".$colorvalue."'> Color me happy.</span>";
        ?>
    

    显然,如果没有一些CSS,这是行不通的,他没有提到这一点。

4 个答案:

答案 0 :(得分:1)

myform.html:

<form action="ColorMe.php" method="post">
<select name="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
<br/>
<input type="submit" value="Set Color"/>
</form>

ColorMe.php:

<?php
//Session is started for a PHP cookie to be resumed/created:
session_start();

//Gets the color from the form:
$Color = $_POST['color'];

//Sets the color to the PHP session UserColor:
$_SESSION['UserColor'] = $Color;

//Redirects to whatever page you want
header("Location: /MyPage.php");

MyPage.php:

<?php 
//Starts the session again to make the PHP session resources available:
session_start();

//Checks if the PHP session variable has been set:
if(isset($_SESSION['UserColor'])){

//If it is set: make $Color be whatever user selected:
$Color = $_SESSION['UserColor'];

}

//Otherwise, if not set:
else{

//Default color will be black:
$Color = "black";
}
?>
<html>
<head>
<title>My Colored Page</title>
</head>
<body>
<h1 style="color: <?php echo $Color; ?>;">Colored Text</h1>
<p style="color:<?php echo $Color; ?>;">Colored paragraph.</p>
</body>
</html>

这是你想要的吗? :)

答案 1 :(得分:1)

我认为你是PHP和HTML的新手,所以我会为你提供非常简单的解决方案,而且目前我还是祝你一切顺利

<?php
if (isset($_POST['colorchoice'])) {
$colorvalue =$_POST['colorchoice'];      
print "<span ><font color=".$colorvalue.">Color me happy.</font></span>";
      }else{}    ?>
<form action="#" method=post>

<p> <select  name="colorchoice" size="1">
<option value="redtext">Red</option>
<option value="bluetext">Blue</option>
<option value="greentext">Green</option>
</select>
</p>
<p>
<input type="submit" value="Submit Information"></form>
</p>

注意:

当你开始编码时确保你打开标签关闭它,在你的代码中你忘了打开表格标签,所以要小心

答案 2 :(得分:1)

color.html文件代码:

<form action=color.php method=post
<select name="colorchoice">
<option value="red">Red</option>
<option value="blue">Blue</option
<option value="green">Green</option>
</select>
<input type="submit"/>
</form>

color.php文件代码:

<?php
$colorvalue=$_POST['colorchoice'];
echo "<span style='color:". $colorvalue."'>Color me happy.</span>";
?>

答案 3 :(得分:0)

你可以这样做

<?php $colorvalue = $_POST['colorchoice'];
if($colorvalue == 'red'){?>
   <span style="color:red"> Red text </span>
 <?php } ?>
 if($colorvalue == 'green'){?>
   <span style="color:green"> green text </span>
 <?php } ?>

显然,你可以为每种颜色添加单独的类而不是内联css。并根据您的愿望制定其他条件