当您单击按钮“提交”时,我希望一个值同时显示在网站上。如果再次单击它,则应该显示数组中的另一个值,依此类推。
我看不到我在做什么错。现在,当我单击按钮时,什么也没有显示。
我创建一个按钮。
<form action="index.php" method="post">
<input type="submit" name="randomRoad" value="Submit">
</form>
我的PHP代码
<?php
if($_POST['randomRoad'] === 'Submit'){
$roadDirections = array(
"straight",
"straight",
"straight",
"straight",
"left",
"left",
"left",
"left",
"right",
"right",
"right",
"right",
);
$correct = true;
$userGuess = $roadDirections;
while ($correct == false) {
$random = mt_rand(0, count($userGuess) -1);
$guessed = $userGuess[$random];
echo "<p>" . $guessed;
if ($guessed == $roadDirections[$correct]) {
$correct = true;
array_splice($userGuess, $random, 1);
echo " YES!";
}
else {
echo " NO!";
}
}
}
它还应根据值是否正确来回显“是”或“否”。
您永远不会进入while循环,因为在循环之前设置的$ correct变量的值等于true。