[英]PHP: How do I find the order in which the check boxes were selected?
I have a PHP code like the one below to see which all players were selected, using a checkbox and a table:我有一个像下面这样的 PHP 代码来查看所有玩家都被选中了,使用一个复选框和一个表格:
<?php
$players = $crud->readPlayers();
if (mysql_num_rows($players) > 0)
{
while ($row = mysql_fetch_array($players))
{
?>
<tr>
<td><input type="checkbox" name="player[]" value="<?php echo $row['player_code'] ?>"></td><td> <?php echo $row['player_name'] ?></td>
</tr>
<?php
}
}
else{
?><tr><td colspan="2"><center>No to select. Please add before creating a team!</center></td></tr>
<?php
}
?>
This form is handled by another page as:此表单由另一个页面处理为:
if(isset($_POST['player'])){
$i=1;
foreach($_POST['player'] as $player_code){
$crud->addTeam($team_id, $player_code,$i++);
}
where the third parameter passed to addTeam()
is supposed to be the order of players, or the number assigned to the player.其中传递给
addTeam()
的第三个参数应该是球员的顺序,或者分配给球员的号码。 Although I get all selected players in the team, I can view the team and players from terminal, I am unable to number them correctly as the order of selection of check boxes, ie, if checkbox with player name 'a' printed next to it is selected after the one with name 'b', it should get a number 2, and not 1. Then, I have to list the whole team in increasing order of player number.虽然我得到了球队中所有被选中的球员,但我可以从终端查看球队和球员,但我无法将他们正确编号为复选框的选择顺序,即如果复选框旁边印有球员姓名“a”在名称为 'b' 的那个之后被选中,它应该得到一个数字 2,而不是 1。然后,我必须按照玩家编号的升序列出整个团队。
player_code
is used to store player details in another table, and can vary accordingly, ie, player named 'a' need not have player_code
set as 1. player_code
用于将玩家详细信息存储在另一个表中,并且可以相应地变化,即名为“a”的玩家不需要将player_code
设置为 1。
How to I get the order of selection of these check boxes and assign number to players accordingly, without using anything except HTML and PHP ?如何获得这些复选框的选择顺序并相应地为玩家分配编号,而不使用除 HTML 和 PHP 之外的任何内容?
I would suggest adding JavaScript to the checkboxes checked/click event.我建议将 JavaScript 添加到复选框选中/点击事件中。 So everytime users click one chk, you could store the player name in another field (maybe hidden?).
因此,每次用户单击一个 chk 时,您都可以将玩家名称存储在另一个字段中(可能隐藏?)。 In that way you will be generating a list of player names in the exact order they were clicked, in that hidden field.
通过这种方式,您将在该隐藏字段中按照点击的确切顺序生成玩家姓名列表。
Browsers will also submit form data in the order in which it appears in the form .浏览器也将按照它在表单中出现的顺序提交表单数据。 There is no information about what order controls were selected in.
没有关于选择的顺序控制的信息。
A plain HTML approach would be to have a grid of radio buttons.一个简单的 HTML 方法是有一个单选按钮网格。 eg each row represents a player and each column a position.
例如,每一行代表一个球员,每一列代表一个位置。
You would need to write PHP that would validate the submitted data to make sure that any given player wasn't selected multiple times.您需要编写 PHP 来验证提交的数据,以确保没有多次选择任何给定的玩家。
Alternatively, you could require that one player be selected (and submitted to the server) at a time.或者,您可以要求一次选择一名玩家(并提交给服务器)。
Each time the form is submitted, you return a new form with the selected players listed in hidden inputs and omitted from the choices given to the user.每次提交表单时,您都会返回一个新表单,其中选定的玩家列在隐藏输入中,并从提供给用户的选择中省略。
This could be rather slow though.不过,这可能会相当慢。
Violating your HTML+PHP only constraint, you could use JavaScript.违反您的 HTML+PHP only 约束,您可以使用 JavaScript。
Listen for click events on the checkboxes and store the values in a hidden input (eg as a comma separated list).侦听复选框上的单击事件并将值存储在隐藏输入中(例如作为逗号分隔列表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.