0

I am creating an HTML form and I have a textbox in that. 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).

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 ..:)

3
  • What have you tried beside from the HTML snippet you've posted? Especially on PHP side? Commented Mar 31, 2013 at 23:32
  • You can either do this with JavaScript directly or with XHR (AJAX) in JavaScript by requesting a PHP page, feeding it a POST or GET to give you feedback on the selected checkboxes. Commented Mar 31, 2013 at 23:34
  • 1
    All your <input>s have the same ID. IDs must be unique. Commented Mar 31, 2013 at 23:36

2 Answers 2

1

Assuming you are using POST as the method for sending the form those checkboxes are in, you can get the values array with $_POST['Intensive'].

Sign up to request clarification or add additional context in comments.

1 Comment

Assuming the OP does not know very much on the matter, this answer might not suffice as constructive enough to assist the OP in what it tries to achieve.
0

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.

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:

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

1 Comment

Hi @lbu ,What u said I have implemented that ,I am able to echo the value but its not going to the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.