Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php检查输入是否与数组中当前显示的项匹配_Php_Arrays_Search_Input - Fatal编程技术网

使用php检查输入是否与数组中当前显示的项匹配

使用php检查输入是否与数组中当前显示的项匹配,php,arrays,search,input,Php,Arrays,Search,Input,我有一个texfield$input和一个带有字符串的数组$word。我正在洗牌数组,并显示用户必须匹配的$words数组中的洗牌字符串 如果混洗(混洗字符串也是当前显示的字符串)字符串是hello,则用户必须键入hello,然后一条消息说“正确!”或错误(如果与100%不匹配) 那么,如何简单地检查用户输入是否等于$words数组中当前显示的字符串?我找了很多,但什么也没找到 当用户键入相应的单词时,将显示数组中的一个新“随机”单词,并且必须正确键入,如图所示。节目一直这样进行 我试过这个:

我有一个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

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

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

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

    if ($input === $toMatch) {
        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>
并将if语句更改为:

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

您当前的代码无效?请同时显示您的表单代码。基本上你想要什么?您希望用户给定的输入在数组中匹配,如果是,则匹配,否则不匹配?是否正确?请检查更新…此代码仅适用于第一个显示字符串。但该计划应该继续有效。我的意思是,应该出现一个新词,用户必须匹配它。你能发布视图吗?它看起来怎么样?你需要显示什么@me72921@me72921您必须添加javascript和ajax才能使某些东西“活起来”——这都是后端,只需处理一次就可以了。@Crembo,我一定会按照你的建议在Javascript和Ajax中实现这一点。这正是我所需要的!
if ($input === $_SESSION['shownWord']) { }