简体   繁体   English

如何获得选中复选框的对应值

[英]How to get the selected checkbox corresponding value

I have a dynamic form with corresponding check box with each row. 我有一个动态表格,每一行都有对应的复选框。

<?php 
  $q2 = mysql_query("SELECT * from plan ORDER BY id ASC");
  while($result=mysql_fetch_array($q2))
   {
?>

<tr class="phbg">
 <td width="50%"><?php echo $result['plan_name'];?></td>
 <td width="25%" class="pc"><? echo '$';echo $result['price'];?></td>
 <td width="25%" class="pc"><input type="hidden" name="id[]" id="id" value="  <?php echo $result['id'];?>">
 <input type="checkbox" name="check_list[]" value="<? echo $result['price'];?>"></td>
</tr>   
<?php  } ?>

Plan    Price   Choose
PHP       $3     []
.Net      $2     []
C#        $6     []
Java      $2     []
C++       $10    []
ROR       $2     []

User can select the multiple options. 用户可以选择多个选项。 At last will show the total of the selected plans. 最后将显示所选计划的总数。 For total I redirected that page to total.php page. 总的来说,我将该页面重定向到total.php页面。 There I did the total of 在那里我总共做了

the selected plans. 所选计划。 On total.php Page I want to get the corresponding Id value or the Plan Name of the selected plans. 在total.php页面上,我想获取相应ID值或所选计划的计划名称。 I just get the id the Last Column but not for 我只获得ID的最后一栏,但没有

the selected columns. 选定的列。 Then after that from total.php page user will fill his/her the details and submit the details along with the selected plans and price. 然后,在total.php页面中,用户将填写他/她的详细信息,并提交详细信息以及所选的计划和价格。 How can I retrieve the id of the selected checkbox Value to show the plans. 如何检索所选复选框的ID以显示计划。

You approach is wrong: 您的方法是错误的:

<input type="checkbox" name="check_list[]" value="<? echo $result['price'];?>">

As only checked checkboxes get sent to the server, you will not be able to identify the checked box by its ID; 由于仅将选中的复选框发送到服务器,因此您将无法通过其ID识别选中的复选框。 if only the third is checked, its key in the array will be 0 . 如果仅选中第三个,则其在数组中的键将为0 And as you are using the monetary value as its value, you will not be able to identify the checked box by the value as the values of different boxes can be the same. 而且,由于您使用货币值作为其值,因此您将无法通过该值来标识复选框,因为不同框的值可以相同。

In short: You will not be able to identify the checked boxes when the form is sent in. 简而言之:发送表单时,您将无法识别复选框。

To correct that you need to make or the name (key on the server) or the value unique so that you can link it to your database. 要更正您需要输入的名称名称(服务器上的键) 值,以便可以将其链接到数据库。

An easy way to do that for the ID, would be to set the ID in the loop: 一种简单的ID设置方法是在循环中设置ID:

<input type="checkbox" name="check_list[<?php echo $result['id'];?>]" value="<? echo $result['price'];?>">
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^ set the key manually

Now you can loop over $_POST['check_list'] and the key in that foreach loop will contain the ID of the item in the database: 现在,您可以循环$_POST['check_list']并且该foreach循环中的键将包含数据库中项目的ID:

foreach ($_POST['check_list'] as $key => $value) {
  //                             ^^^^ the key
  ...

You could of course use the value in the same way. 您当然可以以相同的方式使用该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM