【问题标题】:How can I display my input php on the screen?如何在屏幕上显示我的输入 php?
【发布时间】:2019-07-29 10:27:09
【问题描述】:

我必须分配一个任务,我必须处理以下事情: 制作一个数组“颜色”并用 10 个颜色名称填充它。 创建一个在表格中显示数组内容的脚本。 创建一个脚本,测试输入的颜色是否存在于颜色数组中,并返回是否存在。我现在的问题是我不知道如何在输入中返回正确答案。这是我的代码:

<?php
// different colors:
$color = array(
    '#99ed5c' => 'green',
    '#9c9991' => 'grey',
    '#17ffb6' => 'blue',
    '#a21647' => 'purple',
    '#fbd8c1' => 'light-pink',
    '#1f33a3' => 'dark-blue',
    '#7e0509' => 'red',
    '#f7d856' => 'yellow',
    '#3b0b1b' => 'violet',
    '#de4405' => 'orange'
);

function build_table($color){
    // start table
    $html = '<table>';

    // header row
    $html .= '<tr>';
    foreach($color as $key=>$value){
        $html .= '<th>' . htmlspecialchars($key) . '</th>';
    }
    $html .= '</tr>';

    // data rows
    foreach( $color as $key=>$value) {
        $html .= '<tr>';
        $html .= '<td>' . htmlspecialchars($key) . '</td>';
        $html .= '<td>' . htmlspecialchars($value) . '</td>';
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
}

echo build_table($color);



echo "fill in a color: <input type='text' name='kleur'>";
echo "<input type='submit'> <br>";



if (in_array("green", $color))
{
    echo "Match found";
}
else
{
    echo "Match not found";
}

【问题讨论】:

  • 你想把它放在这个领域吗? &lt;input type='text' name='kleur'&gt;
  • 是的。当我按下提交按钮时,如果找到匹配项或未找到匹配项,它必须显示图像。
  • 如果您尝试获取提交的值,那么您可以在echo "&lt;form method='post'&gt;fill in a color: &lt;input type='text' name='kleur'&gt;"; echo "&lt;input type='submit'&gt;&lt;/form&gt; &lt;br&gt;"; if (in_array($_POST['kleur'], $color)) { echo "Match found"; } else { echo "Match not found"; } 之类的表单中包含文本字段

标签: php arrays forms foreach colors


【解决方案1】:

你说对了。

为了处理用户输入,您需要使用表单处理。即从客户端向服务器端提交 from,Read more info here

所以你需要做类似的事情:

<?php
// different colors:
$color = array(
    '#99ed5c' => 'green',
    '#9c9991' => 'grey',
    '#17ffb6' => 'blue',
    '#a21647' => 'purple',
    '#fbd8c1' => 'light-pink',
    '#1f33a3' => 'dark-blue',
    '#7e0509' => 'red',
    '#f7d856' => 'yellow',
    '#3b0b1b' => 'violet',
    '#de4405' => 'orange'
);

function build_table($color)
{
    // start table
    $html = '<table>';

    // header row
    $html .= '<tr>';
    foreach ($color as $key => $value) {
        $html .= '<th>' . htmlspecialchars($key) . '</th>';
    }
    $html .= '</tr>';

    // data rows
    foreach ($color as $key => $value) {
        $html .= '<tr>';
        $html .= '<td>' . htmlspecialchars($key) . '</td>';
        $html .= '<td>' . htmlspecialchars($value) . '</td>';
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
}

echo build_table($color);
?>


    fill in a color:
    <form method="post" action="">
    <input type='text' name='kleur'>
    <input type='submit' name='submit'> <br>
    </form>

<?php
if (isset($_POST['submit'])) {
    if (empty($_POST['kleur'])) {
        echo "You left the color input blank.";
    } else {
        $inputColor = $_POST['kleur'];
        if (in_array($inputColor, $color)) {
            echo "Match found";
        } else {
            echo "Match not found";
        }
    }
}
?>

请注意,您可以关闭PHP标签以正常打印出HTML,而无需回显。

【讨论】:

  • 是的!谢谢,就是这个!我知道我需要在其中输入一个表格。但不知道如何将它与它需要显示在我的屏幕上的事实联系起来。我想我应该把输入放在一个回声中。非常感谢!
【解决方案2】:

要设置输入的值,只需像这样设置value 参数:

<input type='text' name='kleur' value='<?= $value ?>'>

或者在你的情况下

echo "fill in a color: <input type='text' name='kleur' value='" . $color . "'>";

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 2021-06-22
    • 1970-01-01
    • 2016-02-02
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多