mardi 29 décembre 2015

Checkbox(es) clicked will show another input field, how to put it in the database?

I have a working code where if I select multiple checkboxes, it will be saved in the database in separate rows (it is required). Also, I have a working code where if it checks a certain checkbox, another input field will show. Now, how do I save the checkbox value along the other input field that comes with it in the database?

This is the form:

<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>

<div class = "second">

<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"> Document Request(s) <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">

<input type="checkbox" name="docs[]" id="doc1" value="Certificate of Residency" /> Certificate of Residency
<br/>
<div class = "col-xs-3">
    <input name="d1" class="form-control col-md-7 col-xs-12" required="required" type="number">
</div>

<div class = "clearfix"></div>

<input type="checkbox" name="docs[]" id="doc2" value="Barangay Clearance"  /> Barangay Clearance                                                    
<br />
<div class = "col-xs-3">
    <input name="d2" class="form-control col-md-7 col-xs-12" required="required" type="number">
</div>

    <div class = "clearfix></div>
</form>

This is my php code (this works; it saves multiple checkbox values):

<?php
include 'config.php';

if (isset($_POST['documentRequest']))
{

 $chkbox = $_POST['docs'];

 $i = 0;

 foreach($chkbox as $chk1)  
   {  
       $query = mysqli_query($conn, "INSERT INTO document(typeOfDoc) VALUES ('$chk1');"); 
   }  
 echo "Checkbox value is successfully submitted.";
 } ?>

This is the picture of the form: enter image description here

Can you all please help me? How do I save this? Thank you.




Aucun commentaire:

Enregistrer un commentaire