This is the Jquery Code I have used for creating two new Input Fields every time User clicks Add more Button
var i=1;
$('#addMoreHighlights').on('click',function(event){
//prevent default action
event.preventDefault();
var clone = '<div data-count="'+i+'" ><br/><hr/><div class="form-group" ><label>Trip-Highlight Title:</label><input type="text" value="" name="trip_highlightTitle[]" class="form-control clearHiglights" id="" placeholder="Enter Trip Highlight\'s Title"></div><div class="form-group"><label>Trip-Highlight Image</label><div class="input-group"><input class="form-control clearHiglights" placeholder="Upload Image For The Highlight" name="trip_highlightImage[]" value="" type="text" id="trip_highlightImage"><span class="input-group-addon" style="background: #3C8DBC"> <a style="cursor: pointer;" onclick="BrowseServer("trip_highlightImage");" ><span style="color: #FFF;">Select Image</span></a></span></div></div><button style="float: right; margin: 5px;" data-removeCount="'+i+'"class="removeMoreHighlights btn">Remove</button></div>';
$('#tripHighlights').append(clone);
i++;
});
This newly created element gets attached to the already existing form elements.This is my already existing input fields (html)
<div id="tripHighlights">
<div class="form-group" >
<label>Trip-Highlight Title:</label>
<input type="text" value="" name="trip_highlightTitle[]" class="form-control clearHiglights" id="" placeholder="Enter Trip Highlight's Title">
</div>
<div class="form-group">
<label>Trip-Highlight Image</label>
<div class="input-group">
<input class="form-control clearHiglights" placeholder="Upload Image For The Highlight" name="trip_highlightImage[]" value="" type="text" id="trip_highlightImage">
<span class="input-group-addon" style="background: #3C8DBC">
<a style="cursor: pointer;" onclick="BrowseServer('trip_highlightImage');" >
<span style="color: #FFF;">Select Image</span>
</a>
</span>
</div>
</div>
</div>
<button id="addMoreHighlights" class="btn">Add-More</button>
All of this exists within the tag But still during post I only get the input from fields that are initially generated in the DOM and not from the ones that is generated using jquery
This is my post result:
'trip_highlightTitle' =>
array (size=1)
0 => string 'title 1' (length=7)
'trip_highlightImage' =>
array (size=1)
0 => string 'image1' (length=6)
The actual result I expect is:
'trip_highlightTitle' =>
array (size=1)
0 => string 'title 1' (length=7)
1 => string 'title 2' (length=7)
'trip_highlightImage' =>
array (size=1)
0 => string 'image1' (length=6)
1 => string 'image2' (length=6)