I am trying to add group in input text in dynamically generated fields using jquery.
I want form to be like this here data in clnbox generates via jquery.
<form name="frm" method="post" action"">
First-name:<input type="text" name="firstname" >
<div class="cust_box">
<div id="clonedInput1" class="clonedInput">
Age:<input type='text' name="" data-name="[age]">
Location:<input type='text' name="" data-name="[lcoation]" >
</div>
</div>
<div class="clnbox">
Age:<input type='text' name="data[1][age]" data-name="[age]">
Location:<input type='text' name="data[1][lcoation]" data-name="[lcoation]" >
Age:<input type='text' name="data[2][age]" data-name="[age]">
Location:<input type='text' name="data[2][lcoation]" data-name="[lcoation]" >
</div>
<input type="submit" value="submit" name="datafill">
</form>
Here is the code with jquery:
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
jQuery(document).ready(function($){
jQuery('.clone').change(function(){
$('.clnbox .clonedInput').remove();
for (var i = 0; i <(this).value ; i++){
var data_attr='data['+i+']'+($(".clonedInput input").attr('data-name'));
cncate = data_attr;
$(".cust_box > .clonedInput").clone().appendTo(".clnbox");
$(".cust_box input").attr('name',cncate);
}
});
});
</script>
<select class="clone" name="clone_time">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<form name="frm" method="post" action"">
First-name:<input type="text" name="firstname" >
<div class="cust_box">
<div id="clonedInput1" class="clonedInput">
Age:<input type='text' name="" data-name="[age]">
Location:<input type='text' name="" data-name="[lcoation]" >
</div>
</div>
<div class="clnbox"></div>
<input type="submit" value="submit" name="datafill">
</form>
I need array output like this:
Array
(
[firstname] => lockesh
[data] => Array
(
[1] => Array
(
[age] =>
[lcoation] =>
)
[0] => Array
(
[age] => 33
[lcoation] => virar
)
)
[datafill] => submit
)
Why are you confusing the inputs so much? Do it simple, this way: