【问题标题】:check if input matches current shown item in array with php用php检查输入是否与数组中当前显示的项目匹配
【发布时间】:2015-08-17 08:33:13
【问题描述】:

我有一个 texfield $input 和一个带有字符串的数组 $word。我正在对数组进行洗牌并显示来自$words 数组的用户必须匹配的洗牌字符串。

如果洗牌(洗牌的字符串也是当前显示的字符串)字符串是hello,用户必须输入hello,然后一条消息说“正确!”或wrong!(如果不匹配 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 arrays search input


【解决方案1】:

就像把你需要匹配的词赋给一个变量然后比较一样简单:

<?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>";
    }
}

【讨论】:

  • 此代码仅适用于第一个显示字符串。但是该程序应该继续工作。我的意思是,应该出现一个新词并且用户必须匹配它。
  • 能否请您发布视图它的外观以及您需要显示的内容@me72921
  • @me72921 你必须添加 javascript 和 ajax 才能使某些东西“活”起来——这都是后端,它只处理一次就可以了。..
  • @Crembo,我一定会按照您的建议在 Javascript 和 Ajax 中执行此操作。
【解决方案2】:

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

<?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']) { }  

【讨论】:

  • 正是我需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2022-11-03
  • 1970-01-01
相关资源
最近更新 更多