PHP如何通过表单提交冻结随机数

时间:2015-08-19 02:23:31

标签: php

我想要一个程序来生成一个随机数,打印出该数字,并在用户输入表单字段时保持相同的数字。

现在,以下代码始终刷新随机数生成。

我不知道周围的代码可能包含初始值,因此它不会改变。

$x = rand(5,50);

echo $x."<br><br>";

echo "Enter the number you see above:";

function form1 () {
    function test_input_1($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    ?>

    <form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <input type="text" name="user_input1" value="<?php if(isset($_POST['user_input1'])) { echo htmlentities ($_POST['user_input1']); }?>"/><br>
        <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
    </form>

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST['user_input1'] !="") {
            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.

            return $user_input1;
        }
    } 
}

form1();

2 个答案:

答案 0 :(得分:0)

如此(根据我的评论)。当然这是假设Error 5 The tag 'LookUpField' does not exist in XML namespace 'http://ui.example.com/xaml/touch'. Line 7 Position 14. D:\src\prototype\lookup-control\lookup-control\MainWindow.xaml 7 14 lookup-control 是随机数的字段。

user_input1

我个人会亲自取消<?php function getRandom(){ return rand(5,50); } //function form1 () { function cleanInput($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $x = getRandom(); echo $x."<br><br>"; echo "Enter the number you see above:"; ?> <form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post"> <input type="text" name="user_input1" value="<?php echo isset($_POST['user_input1']) ? cleanInput($_POST['user_input1'] ) : ''; ?>"/><br> <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/> </form> <?php /*? what's this for - but I would change the first if in unneeded if isset($_POST['user_input1'])) then obviously it should be request method in any case it returns to oblivion below here. besides shouldn't it be above the form, just as easy to call in the form echo $_POST part, no? if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_POST['user_input1'] !="") { $user_input1 = test_input_1($_POST["user_input1"]); // more variables here, per line -- and add them to the ="" above. return $user_input1; } } } form1(); <-- oblivion as it's not setting anything. */ ?> 功能,因为它会降低可读性,在这个例子中实际上是不必要的。

答案 1 :(得分:0)

如您所知,HTTP是无状态协议。已创建Cookie和会话以跟踪用户(或请求)。您必须在代码中使用会话来释放随机数。我写了一个简单的代码并添加到你的例子的开头。

{{1}}