简体   繁体   English

在PHP中获取HTML复选框的值

[英]Getting values of HTML checkboxes in php

I am creating an HTML form and I have a textbox in that. 我正在创建一个HTML表单,并且在其中有一个文本框。 My requirement is that a user can check multiple check boxes, and then I have to get all checked values and then send the values to the database (I want to use PHP). 我的要求是用户可以选中多个复选框,然后必须获取所有选中的值,然后将值发送到数据库(我想使用PHP)。

Here is my code for within the textbox 这是我在文本框中的代码

     $Intensive=$_POST['Intensive'];
  $Intensive_count=count($Intensive);
  $i=0;
  //$count=0;
    //$index;
  $max=0;
  //$index;
   While($i < $Intensive_count)
   {
    if($Intensive[$i]=="High frequency ventilation" )
   {
   $Intensive_score=4; 
  }
     elseif($Intensive[$i]=="Mechanical ventilation with muscle relaxation")
  {
   $Intensive_score=4;
  }
  elseif($Intensive[$i]=="Mechanical ventilation")
  {
   $Intensive_score=3;
  }
  elseif($Intensive[$i]=="CPAP")
  {
   $Intensive_score=2;
  }
  elseif($Intensive[$i]=="supplemental oxygen")
  {
   $Intensive_score=1;
  }

 if($Intensive_score>$max)
  {
   $max=$Intensive_score;
   $index=$i;
 }
  $i++;
 }

Now with the above code I am able to echo the value ,but the record is not going to the database. 现在,使用上面的代码,我可以回显值了,但是记录不会发送到数据库。

$sql1="insert into Form2 values('$Medical_Record_Id','$sub1','$Question1','4','$Intensive[$index]','$max')";

 mysql_query($sql1);

Could anybody tell me how to go about it. 谁能告诉我该怎么做。

Thanks ..:) 谢谢 ..:)

假设您使用POST作为发送这些复选框所在表格的方法,则可以使用$_POST['Intensive']获得values数组。

I would recommend you use integers for the value instead of long strings, also an ID is supposed to be unique, so modify your ids. 我建议您使用整数代替长字符串,并且ID应该是唯一的,因此请修改ID。

HTML: HTML:

<input type="checkbox" name="Intensive[]" id="r1" value="1">supplemental oxygen<br>
<input type="checkbox" name="Intensive[]" id ="r2" value="2">supplemental oxygen<br>
<input type="checkbox" name="Intensive[]" id="r3" value="3">Mechanical ventilation<br>
<input type="checkbox" name="Intensive[]" id="r4" value="4">Mechanical ventilation with muscle relaxation<br>
<input type="checkbox" name="Intensive[]" id="r5" value="5">High-frequency ventilation

PHP: PHP:

foreach($_POST['Intensive'] as $data) {// Or $_GET
    if ($data == 1){
      /// do so and so
    }
    if ($data == 2){
      /// do so and so
    }
     ... and so on.
}

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

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