着色用户输入的文本取决于颜色选择PHP

时间:2016-05-02 19:57:28

标签: php function

我被要求创建一个包含表单的PHP页面。该表单应允许用户输入单词并选择颜色。根据他们的选择,页面应该以他们选择的颜色回显用户的单词。我开始尝试实施它,但遇到了一些问题。我的代码如下:

<?php
echo "
<html> 
<head> 
  <title>Colour Text</title> 
</head> 
<body> 
  <form name='colour_text' onsubmit='colour_text()' method='POST'>
    Word: <input type='text' name='word' /><br />
    Choose the colors: 
      <input type='radio' name='colour' value='bk' checked='checked' /> Black
      <input type='radio' name='colour' value='rd' /> Red
      <input type='radio' name='colour' value='be' /> Blue
      <input type='radio' name='colour' value='yw' /> Yellow
      <input type='submit' />
  </form>
</body>
</head>
</html>";

function colour_text($word, $colour)
{

$word = $_POST['word'];
$colour = $_POST['colour'];

    if ($colour == 'bk' ) 
    {
        echo '<div style=\"color:black;\">$word</div>';
    }
    if ($colour == 'rd' ) 
    {
        echo '<div style=\"color:red;\">$word</div>';
    }
    if ($colour == 'be' ) 
    {
        echo '<div style=\"color:blue;\">$word</div>';
    }
    if ($colour == 'yw' ) 
    {
        echo '<div style=\"color:yellow;\">$word</div>';
    }
}
?>

作为PHP的新手,我搜索并发现了一些类似的问题,但没有任何内容直接适用于此。

2 个答案:

答案 0 :(得分:0)

<?php
function colour_text() {
    // $_POST is global
    $word = $_POST['word'];

    switch ($_POST['colour']) {
        case 'bk': $colour = "black";
        case 'rd': $colour = "red";
        case 'be': $colour = "blue";
        case 'yw': $colour = "yellow";
        default: $colour = "black";
    }

    $result = "<div style=\"color:".$colour."\">".$word."</div>";

}

IF (isset($_POST['submit'])) {
    $result = colour_text();

    }ELSE{ // form not submitted
    // form ......
    $result = "<form action=\"".$_SERVER["PHP_SELF"]."\" name=\"colour_text\" method=\"POST\">
    Word: <input type=\"text\" name=\"word\" value=\"\" /><br />
    Choose the colors: 
    <input type=\"radio\" name=\"colour\" value=\"bk\" checked=\"checked\" /> Black
    <input type=\"radio\" name=\"colour\" value=\"rd\" /> Red
    <input type=\"radio\" name=\"colour\" value=\"be\" /> Blue
    <input type=\"radio\" name=\"colour\" value=\"yw\" /> Yellow
    <input type=\"submit\" name=\"submit\" value=\"\" />
</form>";
    }

echo("<html> 
<head> 
    <title>Colour Text</title> 
</head> 
<body> 
".$result."
</body>
</head>
</html>");
?>

答案 1 :(得分:0)

你想要这样的东西,因为时间不是很好的代码,但它可以是你的例子的好教程,并将帮助你学习PHP

注意您的代码

  1. 使用数组或其他类似数据类型
  2. 始终检查变量是否设置
  3. 即使你可以关闭,也不要将HTML与PHP混合,即使你可以关闭?&gt; php标签和写html,但它也不推荐
  4. 您只能在DOM事件上附加JAVASCRIPT函数

    $colors = ['black', 'red', 'blue', 'yellow'];
    
    echo "
    <html> 
    <head> 
      <title>Colour Text</title> 
    </head> 
    <body> 
      <form name='colour_text' method='POST'>
        Word: <input type='text' name='word' /><br />
        Choose the colors: ";
    $checked = " checked='checked' ";
    foreach ($colors as $color) {
        echo "<input type='radio' name='colour' value='{$color}' {$checked}/> " . ucfirst($color) . " ";
        $checked = '';
    }
    
    echo "      <input type='submit' name='submitButton' />
      </form> ";
    
    if (isset($_POST['submitButton'])) {
        colour_text($_POST['word'], $_POST['colour']);
    }
    
    echo "
    </body>
    </head>
    </html>";
    
    function colour_text($word, $colour) {
        global $colors;
        $word = $_POST['word'];
        $colour = $_POST['colour'];
    
        if (!in_array($colour, $colors)) {
            $colour = 'black';
        }
    
        echo '<div style="color:' . $colour . ';">' . $word . '</div>';
    
    }