xenish
xenish

Reputation: 1424

Dynamically Created Input Field's data not being posted

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)

Upvotes: 1

Views: 300

Answers (2)

Kalish
Kalish

Reputation: 833

well, I used your code to create an example, and I can see the dynamically added fields are also part of the form submission. see if this helps!

$(document).ready(function(){
		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++;
                    });
					$( "form" ).on( "submit", function( event ) {
  event.preventDefault();
  console.log( $( this ).serialize() );
});
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="test" id="test" action="test" method="post">
   <div id="tripHighlights">

                        <div class="form-group" >
                            <label>Trip-Highlight Title:</label>
                            <input type="text" value="" name="trip_highlightTitle[]" class="form-control clearHiglights" id="test1" 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="test2">
                               
                            </div>
                        </div>

                    </div>
                    <button id="addMoreHighlights" class="btn">Add-More</button> 
					
					 <button type="submit" class="boton" > Submit</button>
    <button type="reset" class="boton" style="float:right;"> Cancel</button>
</form>

Upvotes: 1

Tobse
Tobse

Reputation: 747

One thing I've stumbled on is that you're using the same id attribute over and over in your clone HTML. I have corrected this here by adding your i-counter to the element's id attribute and it seems to work: https://jsfiddle.net/8ho414nn/4

var clone = '<div data-count="' + i + '" ><br/><hr/>'
          + '<div class="form-group" ><label>'
          ...
          + 'type="text" id="trip_highlightImage' + i + '">'
          ...
          + '"class="removeMoreHighlights btn">'
          + 'Remove</button></div>';

Upvotes: 1

Related Questions