当两个值彼此相等时,HTML表单上的PHP输入验证

时间:2019-03-06 15:11:11

标签: php html

以下代码用于用户输入数字的表格。

    <input type="number" name="team1" class="inputbox" oninput="validity.valid||(value='')" min="0"> <input type="number" name="team2" class="inputbox" oninput="validity.valid||(value='')" min="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="number" name="team3" class="inputbox" oninput="validity.valid||(value='')" min="0"> <input type="number" name="team4" class="inputbox" oninput="validity.valid||(value='')" min="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="number" name="team5" class="inputbox" oninput="validity.valid||(value='')" min="0"> <input type="number" name="team6" class="inputbox" oninput="validity.valid||(value='')" min="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="number" name="team7" class="inputbox" oninput="validity.valid||(value='')" min="0"> <input type="number" name="team8" class="inputbox" oninput="validity.valid||(value='')" min="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

这些数字被发送到roundThree.php,它比较数字1和数字2,数字3和数字4,依此类推。它找出哪个比另一个高。

$teams = Array($team1, $team2, $team3, $team4, $team5, $team6, $team7, $team8, $team9, $team10, $team11, $team12, $team13, $team14, $team15, $team16);
$winCheck = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for ($x=0; $x < count($teams); $x+=2) {

    if ($teams[$x] > $teams[$x + 1]) {
        winCheck[$x] = 1;
    }

    elseif ($teams[$x] < $teams[$x + 1]) {
        $winCheck[$x + 1] = 1
    }
    else {

    }

};

我想使用输入验证来防止用户输入彼此相等的两个数字。我可以放入else {}进行某种验证,以使用户返回页面并让他们重新输入两个不同的数字吗?

1 个答案:

答案 0 :(得分:1)

使用array_unique()允许您返回没有重复值的数组。然后将新的唯一数组与原始数组进行比较。

示例:

if (array_unique($teams) == $teams) {
   // Array is unique
} else {
   header('Location: example.url'); // Bring back to desired page
}