Check if input is the same as currently shown element in array with php

I have a texfield $input

and an array with strings $word

. I will shuffle the array and showing the shuffled string from the array $words

that should fit the user.

If the shuffled (the shuffled string is also the currently displayed string) is the string hello

, the user must enter hello

, followed by the message "correct!" or wrong!

(if it doesn't match 100%).

So how can I just check if the user input is the same as the currently displayed string in the array $words

? I searched a lot for this but couldn't find anything.

When the user enters the appropriate word, a new "random" word from the array is displayed and must be entered correctly as shown. The program continues to work as follows.

I've tried this:

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

      

With this code I am checking if it is inside an array or not, I know, but this is my closest bet.

Here's a screenshot of my mini-program:

enter image description here

Any help is appreciated. Thanks in advance!

+3


source to share


2 answers


If I understand correctly what you are after, I think this is what you are looking for:

<?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>

      

Depending on your needs, it would be better to store a random word in session and then check for the matching word from there, For example:



$_SESSION['shownWord'] = $toMatch;

      

And change the if statement to:

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

      

+1


source


It's as simple as assigning a word to be matched to a variable and then comparing:



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

      

+2


source







All Articles