我有一个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']) { }