简体   繁体   English

PHP:如何找到复选框被选中的顺序?

[英]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.没有关于选择的顺序控制的信息。

The Grid网格

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 来验证提交的数据,以确保没有多次选择任何给定的玩家。

Multiple pages多页

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.不过,这可能会相当慢。

JavaScript JavaScript

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.

相关问题 PHP-如何选择多个复选框并显示所选复选框中的所有值? - PHP - How can i select multiple check boxes and show all the values from the selected check boxes? 如何查看 PHP 中选中了哪些复选框? - How can I see which check boxes are checked in PHP? 如何检查是否选择了文件 <input type=“file”> 在PHP中? - How do I check if a file is selected for <input type=“file”> in PHP? 如何根据选择的字段显示按钮?PHP - How do I show a button depending on which field is selected?PHP 如何使用PHP / MySQLi从数据库填充复选框 - How do I populate check boxes from Database using PHP/MySQLi 如何创建一个包含链接到PHP中不同数组的框的内容页面? - How do I create a content page that contain boxes which link to different arrays in PHP? 我创建了一个php函数,该函数构建了7个复选框,7个产品下拉框和7个与每个产品相关的数量框。 - I've created a php function that builds 7 check boxes, 7 product drop down boxes and 7 quantity boxes which pertain to each prouduct. 我正在尝试识别使用 PHP 从复选框中选择的多个值 - I am trying to identify multiple values which were selected from a checkbox using PHP 当我在数组中找到最大值时,如何找出它的顺序? - When I find the max value in an array how do I find out which order it was in? 如何获取所有关键字并检查我已经选择了哪些关键字? - How do I get all the keywords and check which ones I have already selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM