改变文本的颜色取决于用户选择的PHP, CSS和HTML


Changing color of text depending on user selection with PHP, CSS and HTML

我的作业要求我制作一个颜色选择列表,然后使用PHP在选择的颜色中打印语句。原文如下:

  1. 创建一个带有选择选项列表的HTML表单,其颜色选项如下:红,蓝,绿

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

    Color me happy

使用一个CSS类来设置文本的颜色。

这是他说的:

    <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,这是不行的,而他没有提到这些。

为了你自己,让一切都尽可能简单。

<style>
    .red   { color: red; }
    .green { color: green; }
    .blue  { color: blue; }
</style>
<form>
    <select name="color">
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
    </select>
    <input type="submit" value="Color me!">
</form>
<?php
    if ( isset($_GET['color']) ) {
        print '<span class="' . $_GET['color'] . '">Color me happy.</span>';
    }
?>

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>
这是你想要的吗?:)

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

我认为你是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>

注意:

当你开始编码时一定要打开标签关闭它,在你的代码中你忘记打开表单标签了,所以要小心

你可以这样做

<?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。并根据您的意愿设置其他条件