簡體   English   中英

用於顯示數組中的一個隨機元素的 Html 代碼,但不應為超過 1 個用戶重復

[英]Html code to display one random element from an array but should not be repeated for more than 1 user

最初有不同的代碼/文本存儲在一個數組中。 用戶單擊按鈕並顯示代碼/文本。 重要提示:1) 必須從列表中隨機選擇代碼。 2)代碼是唯一的,不能多次顯示! (例如,如果在提交表單時顯示了一個代碼,則下次提交表單時就不能再顯示該代碼了)。

*這將是一個基本的html js網站

我猜你是和我一樣的學生,當編寫新的東西時,可能很難開始。 這就是為什么我想分享它。

<html>
<body>
<form action="#" method="POST">
    <p> Add 5 unique numbers</p>
    <input type="number" name="0" min="0" max="100">
    <input type="number" name="1" min="0" max="100">
    <input type="number" name="2" min="0" max="100">
    <input type="number" name="3" min="0" max="100">
    <input type="number" name="4" min="0" max="100">
    <button>Send</button>
    <br> 
</body>
</html> // This is our html code. We get the numbers.
<?php
$array=$_POST; // Adding numbers to the array

if(isset($array)) // isset checks the array we set any data otherwise we get an error message
{
    $result=array_unique($array); // checks number differences
    if (count($result)<5)
    {
        echo "All numbers are not unique!";
        die(); // if numbers are same, program ends here
    }
    else{
        for($i=0;$i<5;$i++)
        {
            echo ($i+1)."."."number=".$array[0]."<br>";
            array_shift($array); // every loop it deletes the first index and prints the next one
        }
}
}
?>

最后一部分不是你需要的,你可能需要用 js 或 ajax 來做這件事。 希望它可以成為一個很好的例子。 它還幫助我做我學到的東西。 GL :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM