微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用php检查输入是否匹配当前显示的项目

我有一个texfield $input和一个带有字符串$word的数组.我正在改组数组,并显示用户必须匹配的$words数组中的改组字符串.

如果改组(改组后的字符串也是当前显示的字符串)字符串是hello,则用户必须键入hello,然后显示一条消息“正确!”.或错! (如果与100%不匹配).

那么,如何简单地检查用户输入是否等于$words数组中当前显示的字符串?我为此进行了大量搜索,但找不到任何东西.

用户键入相应的单词时,将显示来自数组的新“随机”单词,并且必须正确输入,如图所示.该程序一直这样.

我已经试过了:

<form method = "post" action = "<?PHP echo htmlentities($_SERVER['PHP_SELF']); ?>">
            <input type = "text" name = "inputfield" id = "inputfield"><br>
            <input type = "submit" name = "submit" value = "TJEK SPELLING" id = "spelling"><br>
        </form>

$word = array("hello", "how", "are", "you", "great", "fine");
shuffle($word);

//The word that has to be matched is shown
echo reset($word);

if (isset($_POST['submit'])) {
            $input = $_POST['inputfield'];
            echo "You typed : <b> $input </b>";
            echo "<br>That was : ";

            if (in_array($input, $word)) {
                echo "<b>Correct!</b>";
            } else{
                echo "<b>Wrong</b>";
            }
        }

我知道用这段代码检查它是否在数组内,但这是我最接近的选择.

这是我的小程序的屏幕截图:

任何帮助表示赞赏.
提前致谢!

解决方法:

如果我正确理解您的追求,我认为这就是您想要的:

<?PHP

if (isset($_POST['inputField']) && isset($_POST['shownWord'])) 
{
    $input = $_POST['inputField'];
    echo "You typed : <b> $input </b>";
    echo "<br>That was : ";

    if ($input === $_POST['shownWord']) {
        echo "<b>Correct!</b>";
    } else{
        echo "<b>Wrong</b>";
    }
}

$word = array("hello", "how", "are", "you", "great", "fine");
shuffle($word);
$toMatch = reset($word);
?>
<p>Enter this word: <?PHP echo $toMatch; ?></p>
<form name ="form" method = "POST">
    <input type="hidden" name="shownWord" value="<?PHP echo $toMatch; ?>" />
    <input type="text" name = "inputField" >
    <input type="submit" value="Submit">
</form>

根据您的需求,最好将随机单词保存到session,然后从那里检查匹配的单词.例如:

$_SESSION['shownWord'] = $toMatch;

并将if语句更改为:

if ($input === $_SESSION['shownWord']) { }  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐