如何使用随机数验证同一页上的表单输入? [英] How to validate form input on the same page by using random numbers?

查看:100
本文介绍了如何使用随机数验证同一页上的表单输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你能帮助我,那会很棒!我正在尝试构建一个简单的页面来显示随机数学问题。答案应该由用户输入,并且应该在同一页面上进行验证,从而向他提供关于成功和失败的反馈。
$ b $问题是,通过提交表单输入到同一页面(我称之为form6.php),页面重新加载并生成一组新的数字 - 这就是为什么解决方案变量是新定义的,不能用于测试用户答案的​​准确性。



这是我迄今为止的代码(正常工作对于固定数字而言,但随着随机数字的产生而失败):

pre $ code><?php
$ number1 = rand(1,100 );
$ number2 = rand(1,100);

解决方案

= $ number1 + $ number2;

echo$ number1 + $ number2 =?;

?>

< form action =form6.phpmethod =post>
您的答案:< br>
< input type =integername =answer>
< input type =Submitvalue =Submit!>
< / form>

<?php
if(isset($ _ POST ['answer'])){
if($ _POST [answer] == $ solution)
{echoThat's right;}
else
{echoThat's wrong!;};
}

?>

非常感谢任何帮助!因为我不是专业的编码员,所以你可以得到更具体的结果,更好!

POST ed表单将重新载入您的PHP脚本,导致生成2个新的 rand()数字。您需要在表单中传递解决方案。



编辑:我更新了答案,以显示使用随机操作数进行验证。这只是通过切换一个随机整数来完成的。请注意,当使用 - / 时,您可能收到非整数( 3 / 58 15 - 86 ),因此您可能需要添加一些自定义逻辑来防止这种情况发生。

 <?php 
$ number1 = rand(1,100);
$ number2 = rand(1,100);

switch(rand(0,3)){
case 0:
$ solution = $ number1 + $ number2;
回声$ number1 + $ number2 =?;
休息;
案例1:

解决方案

= $ number1 - $ number2;
回声$ number1- $ number2 =?;
休息;
案例2:

解决方案

= $ number1 * $ number2;
回声$ number1 * $ number2 =?;
休息;
案例3:

解决方案

= $ number1 / $ number2;
回声$ number1 / $ number2 =?;
休息;
}
?>

< form action =form6.phpmethod =post>
您的答案:< br>< input type =integername =answer>

< input type =Submitvalue =Submit!>
< / form>

<?php
if(isset($ _ POST ['answer'])&& isset($ _ POST ['solution'])){
if( $ _POST [answer] == $ _POST ['solution']){
// Valid
} else {
//错误
}
}
?>


Would be awesome if you could help me out! I am trying to build a simple page to display random math questions. The answers should be typed in by the user and should be validated on the same page, giving him feedback on success and failure.

The problem is that by submitting the form input to the same page (I called it "form6.php") the page reloads and a new set of numbers is generated - that is why the "solution" variable is newly defined and cannot be used to test the accuracy of the user's answer.

This is the code I have so far (works fine for fixed numbers but fails with the random number generation):

<?php
$number1 = rand(1,100);
$number2 = rand(1,100);
$solution = $number1+$number2;

echo "$number1+$number2=?";

?>

<form action="form6.php" method="post">
Your Answer:<br>
<input type="integer" name="answer">
<input type="Submit" value="Submit!">
</form>

<?php
if(isset($_POST['answer'])){
    if ($_POST["answer"] == $solution)
    {echo "That's right";}
    else
    {echo "That's wrong!";};
}

?>

Any help is highly appreciated! Since I am not a professional coder, the more specific you can get, the better!

解决方案

A POSTed form will reload your PHP script, causing 2 new rand() numbers to be generated. You're going to want to pass the solution in the form.

Edit: I updated the answer to show a quick solution for using random operands for verification. This was simply done by switching through a random integer. Please note that when using - or / you may receive non-integer numbers (3 / 58 or 15 - 86), so you may want to add some custom logic to prevent this.

<?php
$number1 = rand(1,100);
$number2 = rand(1,100);

switch(rand(0,3)) {
    case 0:
        $solution = $number1 + $number2;
        echo "$number1+$number2=?";
        break;
    case 1:
        $solution = $number1 - $number2;
        echo "$number1-$number2=?";
        break;
    case 2:
        $solution = $number1 * $number2;
        echo "$number1*$number2=?";
        break;
    case 3:
        $solution = $number1 / $number2;
        echo "$number1/$number2=?";
        break;
}
?>

<form action="form6.php" method="post">
    Your Answer:<br><input type="integer" name="answer">

    <input type="hidden" name="solution" value="<?php echo $solution; ?>">
    <input type="Submit" value="Submit!">
</form>

<?php
if(isset($_POST['answer']) && isset($_POST['solution'])) {
    if ($_POST["answer"] == $_POST['solution']) {
        // Valid
    } else {
        // Incorrect
    }
}
?>

这篇关于如何使用随机数验证同一页上的表单输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆