如何解决如何创建下拉列表并随机显示所选项目之一?
下面的代码段中有4个文本框。您可以在这四个文本框中键入所需的所有内容,然后单击“选择一个”按钮,您将取回在文本框中输入的文本。但是,我想将这些文本框更改为下拉列表,从中可以选择四个不同的团队,并且当您随机单击“选择一个”按钮时,您将获得所选的四个团队之一。>
<?php
error_reporting(0);
if(isset($_POST['submit']))
{
$team1=$_POST['team1'];
$team2=$_POST['team2'];
$team3=$_POST['team3'];
$team4=$_POST['team4'];
$teams=array
(
"1" =>"$team1","2" =>"$team2","3" =>"$team3","4" =>"$team4",);
$random = rand(1,4);
}
?>
<html>
<form action="index.php" method="POST">
Enter the team name<input type="text" name="team1" value="<?php echo $team1 ?>"><br>
Enter the team name<input type="text" name="team2" value="<?php echo $team2 ?>"><br>
Enter the team name<input type="text" name="team3" value="<?php echo $team3 ?>"><br>
Enter the team name<input type="text" name="team4" value="<?php echo $team4 ?>"><p>
<input type="submit" name="submit" value="Pick One!"><br>
</form>
<?php
print $teams["$random"];
?>
</html>
解决方法
如果要使用下拉列表而不是文本框,则可以使用下面的代码:
<!DOCTYPE html>
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<label for="select1">Team 1: </label>
<select name="team1" id="select1">
<option>TeamName1</option>
<option>TeamName2</option>
...
</select><br/>
<label for="select2">Team 2: </label>
<select name="team2" id="select2">
<option>TeamName1</option>
<option>TeamName2</option>
...
</select><br/>
<label for="select3">Team 3: </label>
<select name="team3" id="select3">
<option>TeamName1</option>
<option>TeamName2</option>
...
</select><br/>
<label for="select4">Team 4: </label>
<select name="team4" id="select4">
<option>TeamName1</option>
<option>TeamName2</option>
...
</select><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (!empty($_POST['submit'])) {
$randnumber = rand(1,4);
echo $_POST['team' . $randnumber];
}
?>
</body>
</html>
我希望我的回答对您有用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。