0

For some reason, when I submit my form after initialising the onchange event, the parent id is no longer inserting the id into the database. Instead it inserts 'Select Event'. Is there something simple I'm missing?

FORM

     <select class="form-control" id="parent_id" name="parent_id">
         <option>Select Event</option>
             @foreach (Event::all() as $item)
               <option value="{{ $item->id }}">{{ $item->title }}</option>
             @endforeach
      </select>

     <div id="child_id"></div>

JAVASCRIPT

  $(document).ready(function($){
        $('#parent_id').change(function(){
        $.getJSON("{{ url('api/dropdown')}}", { option: $(this).val() }, 
            function(data) {
                if ( data.success == true) { 
                    $('#busy').hide();
                    $.each(data, function(key, value) { 
                         $('#child_id').append('<div> ' + value.name +'</div>');
                    });

                }
            });

        });

    });

user3189734
  • 665
  • 1
  • 9
  • 27
  • Can you show us the actual FORM tag, too? It's only going to submit the data in your SELECT tag, but it'll be referenced by the ID. Your POST or GET will essentially look like : `parent_id=Select Event&something=somethingelse` – Jason Oct 10 '14 at 13:35
  • Have you any other component on the page with the same id? Try setting value="" for the first option and use $('#parent_id').val() instead of $(this).val() – Ragnar Oct 10 '14 at 13:43

2 Answers2

1

As far as i understand all your option of select are adding at the run time, at the time of posting the value is reset to the default having only one item so. you can use an hidden field as

<input type="hidden" id="hiddenvalue"></input>

and assign an event on the select change that assign the selected value to the hidden field as

$("#parent_id").on("change",function(){
       var $select = $(this);
       $("#hiddenvalue").val($select.val());
});

take the value from hidden field instead of select.

Hope it helps ..........

Mayank
  • 1,351
  • 5
  • 23
  • 42
-1

You should be looking for the :selected element in your option.

see this: Get selected value of a dropdown's item using jQuery

Community
  • 1
  • 1
Jason
  • 3,020
  • 2
  • 24
  • 23