I have created one form with dynamically created fields. and i have a one check box with unique ID . when user selects that check box then only those two fields are visible ("name and age"). and if user uncheck the select box then those fields are hiding . here is the code
<style>
.selectContainer{
display:none;
}
input[type=checkbox]:checked ~ .selectContainer {
display:block;
}
</style>
And The HTML code Is:
<label for="name">Any Accompanying Person ?:</label>
<input type="checkbox" name="person" id="person" >Yes
<div class="selectContainer">
<br>
<label>Person Details</label>
<p>
<div style="padding-left:70px;">
<input type="button" value="Add Person" onClick="addRow('dataTable')" />
<input type="button" value="Remove Person" onClick="deleteRow('dataTable')" />
</div>
</p>
<table style="padding-left:50px;" id="dataTable" class="form" border="1" >
<tbody>
<tr>
<p>
<td><input type="checkbox" name="chk[]" checked="checked" /></td>
<td>
<label>Name</label>
<input type="text" size="20" name="name[]" id="name" >
</td>
<td>
<label>Age</label>
<input type="text" size="20" name="age[]" id="age" >
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
</div>
<h3>Choose Your Payment Option</h3>
<h1>
<div style="padding-left:150px;">
<input type="radio" name="type" value="visa" required value="1" >VISA/MASTER CARD:<br />
<input type="radio" name="type" value="cheque"> CHEQUE/DEMAND DRAFT<br />
<input type="radio" name="type" value="neft">NEFT<br /><br/>
</div>
<label></label>
<input type="submit" name="submit" id="submit" value="submit"><br />
And the php Code is:
if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$intIncrement = 0;
for($l=0; $l < count($name); $l++)
{
if ($age[$l]>=12)
{
$intIncrement++;
}
$user_name="name";
$user_id=123;
$qry="INSERT INTO Accomp_Person (name,age,ref_id, ref_name)
VALUES ('$name[$l]','$age[$l]', '$user_id','$user_name')";
Problem: i am storing name and age of the Accompanying Person .if user checks the check box and add the name and age then its storing in database successfully but the problem is when user came and simply check and then after uncheck the check box (Any Accompanying Person?) then the empty value is storing in database . how to avoid this . i want store those value only check box(Accompanying Person?) is checked. Please help me .