【问题标题】:How to create drop-down lists and randomly show one of the items chosen?如何创建下拉列表并随机显示所选项目之一?
【发布时间】:2020-12-13 04:56:11
【问题描述】:

下面的代码中有 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>

【问题讨论】:

  • 目前尚不清楚您的实际问题是什么。这与下拉菜单有什么关系?
  • 对不起,我试着解释得更好。所以现在,有 4 个输入文本的地方。你可以写任何你想要的,当你点击“选择一个”按钮时,你会得到任何文本您之前输入的内容。我想将这些输入文本位置更改为下拉列表,您可以在其中选择一些团队,当您单击“选择一个”时,您将随机获得 1 个团队。

标签: php html random


【解决方案1】:

如果你想使用下拉列表而不是文本框,你可以使用下面的代码:

<!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>

希望我的回答对你有用

【讨论】:

  • 不幸的是,当我尝试放置更多选项标签时收到错误消息。错误消息是:“未定义索引”(特别是第 48 和 55 行)所以 for 和 rand 有一些问题。我尝试放大它,但仍然效果不佳。
  • @Santiago 对此我深表歉意。这都是我的错。上面这段代码是我刚刚编辑的,现在用就没有问题了。
  • 我不想打扰你这么小的问题,但这仍然工作得很糟糕。当我尝试放大时,我得到一个错误代码,类似于:“未定义的偏移量:4”。所以现在在选项有teamname1和teamname2。我添加了teamname3,当我得到这个(teamname 3)时,我得到这种错误代码。我尝试扩大rand,但我得到相同的错误消息。(注意:未定义的偏移量: 4 在 D:\xampp\htdocs\index.php 第 55 行)
  • @Santiago,我刚刚更改了代码。请再次检查。对于您遇到的错误,我深表歉意。
  • 现在,这是完美的。感谢您的所有努力。
猜你喜欢
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多