PHP中的拼写检查

PHP中的拼写检查,php,html,spelling,Php,Html,Spelling,当用户键入答案时,我如何设计结果?我希望它更生动。谢谢 您需要检查是否定义了POST值: (Notice: Undefined index: word in C:\xampp\htdocs\dyslexia\sample.php on line 15 Sorry, you typed the wrong answer. ) 它可以是以下两种情况之一: 也许你发错了文件 从你的错误中可以看出 (注意:未定义索引:第15行C:\xampp\htdocs\dyslexia\sample.p

当用户键入答案时,我如何设计结果?我希望它更生动。谢谢

您需要检查是否定义了POST值:

  (Notice: Undefined index: word in C:\xampp\htdocs\dyslexia\sample.php on line 15
  Sorry, you typed the wrong answer. ) 

它可以是以下两种情况之一:

  • 也许你发错了文件 从你的错误中可以看出

    (注意:未定义索引:第15行C:\xampp\htdocs\dyslexia\sample.php中的word) 对不起,您键入了错误的答案。)

    而您的表单会发布到页面act_vowel.php“

    您确定您正在发布到正确的页面吗?这也可能是错误的来源(不是说是,但可能是)

  • 在比较变量值之前,您没有检查是否设置了
    $\u POST[“word”]
    变量
  • 试试下面的方法

    if (isset($_POST["word"])) {
        if ($_POST["word"] == "ship" )
        {
            echo "Your answer is correct.";
        }
        else
        {
            echo "Sorry, you typed the wrong answer. Try Again!";
        }
    }
    
    如果(isset($\u POST[“word”])&&$\u POST[“word”]=“ship”){ 回声“你们的答案是正确的。”; }否则{ echo“对不起,您键入了错误的答案(或者可能没有键入:P)。请重试!”; }
    如果所有这些都在同一个PHP页面上,那么所发生的事情是在首次加载页面时检查值,而不是在将值发布到页面后检查值。在尝试响应之前,您需要检查是否存在表单发布:

    if(isset($_POST["word"]) && $_POST["word"]=="ship"){ echo "Your answer is correct."; }else{ echo "Sorry, you typed the wrong answer(Or maybe didnt type it :P). Try Again!"; } 关于你最后的评论:

    当用户键入答案时,我如何设计结果?我希望它更生动

    您可以做的一件事是检查JavaScript而不是PHP中的拼写,这样就不需要表单post。如果希望在用户键入时发生这种情况,您可以响应
    keypress
    事件。例如,使用jQuery:

    if (isset($_POST["check"]))
    {
        if ($_POST["word"] == "ship" )
        {
            echo "Your answer is correct.";
        }
        else
        {
            echo "Sorry, you typed the wrong answer. Try Again!";
        }
    }
    
    这将使用响应更新id为“result”的元素(您将其放在页面的某个位置):

    $('input[name=Word]').keypress(function () {
        if ($(this).val() == 'ship') {
            $('#result').text('Your answer is correct.');
        } else {
            $('#result').text('Sorry, you typed the wrong answer. Try Again!');
        }
    });
    
    
    
    不过,用户在键入时的响应体验可能有点尴尬。在键入单词时,他们会一直被告知错误,直到他们完成为止,这在键入正确答案时有点消极

    更改HTML太多,你的是无效的

    向表单中添加一个id,以便使用JavaScript更轻松地对其进行操作

    <span id="result"></span>
    
    要适应禁用javascript的用户,请将以下内容添加到act_vowel.php文件中

    var form = document.getElementById("form");
    form.onsubmit = function () {
        if (document.getElementsByName("word")[0].value.toLowerCase() == 'ship') {
            alert('Your answer is correct.');
        } else {
            alert('Sorry, you typed the wrong answer. Try Again!');
            return false;
        }
    }
    

    您在发布表单时是否遇到此错误,或者您在加载页面时没有将表单发布到表单中?如果它只是拼写而不是语法,我建议您将
    If($\u post[“word”]=“ship”)
    替换为
    If(strtolower($\u post[“word”])=“ship”)
    @YusafKhaliq你知道如何从这段代码中弹出一个窗口吗?比如,如果用户单击“回答”按钮,结果就会弹出?谢谢!这可以通过javascript实现我会编辑david的答案来为你做这件事我已经回答了你的问题,就像我在Laurent的回答中建议的jQuery选项替换
    $(this.val()
    $(this.val().toLowerCase())
    为了确保检查拼写而不是语法:)可能是三元运算符在这里做得更好…;)顺便说一句+1忽略我的编辑它完全错了它看起来更像是注释:)我应该把它移到注释中吗?然后@NullPoièteè?我想它太长了,但没有问题。你说如果你再加上实际问题会很酷…我在等着在编辑完@NullPoièteè:)后,我会给你投票。虽然很有趣,第二点的数字没有变为2。很奇怪!!谢谢你的鼓励…你不会为一个励志演说家做坏事的,你知道:D
    <span id="result"></span>
    
    <form action="act_vowel.php" method="post" id="form">
         <h2> <font color = "black">  Type the word: </font> </h2> 
        <input type="text" name="word" />
        <input type="submit" name="check" value="Answer" />
    </form>
    
    var form = document.getElementById("form");
    form.onsubmit = function () {
        if (document.getElementsByName("word")[0].value.toLowerCase() == 'ship') {
            alert('Your answer is correct.');
        } else {
            alert('Sorry, you typed the wrong answer. Try Again!');
            return false;
        }
    }
    
    if (isset($_POST["word"])) {
        if (strtolower($_POST["word"]) == "ship" )
        {
            echo "Your answer is correct.";
        }
        else
        {
            echo "Sorry, you typed the wrong answer. Try Again!";
        }
    }