Cheers02's avatar

how to upload file using modal

i want to upload a file using modal in my form this is my modal

<div id="tambah-modal" class="modal fade " role="dialog">
  <div class="modal-dialog modal-lg">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h4 class="modal-title"></h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" role="form">

          <div class="form-group">
            <label class="control-label col-sm-2" for="id2">ID Survey:</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="id2" disabled>
            </div>
          </div>

           <div class="form-group">
            <label class="control-label col-sm-2" for="dokumen">Dokumen:</label>
            <div class="col-sm-10">
              <input type="file" class="form-control" id="dokumen" required="">
            </div>
          </div>
        </form>
      </div>

        <div class="modal-footer">
          <button type="submit" class="btn actionBtn" data-dismiss="modal">
            <span id="footer_action_button2" class='glyphicon'> </span>
          </button>
          <button type="button" class="btn btn-warning" data-dismiss="modal">
            <span class='glyphicon glyphicon-remove'></span> Close
          </button>
        </div>
      </div>
  </div>
</div>

this is my JS

$(document).ready(function() {

  $(document).on('click', '.tambah-modal', function() {

        $('#footer_action_button2').text("Tambah");
        $('#footer_action_button2').addClass('glyphicon-check');
        $('#footer_action_button2').removeClass('glyphicon-trash');
        $('.actionBtn').addClass('btn-success');
        $('.actionBtn').removeClass('btn-danger');
        $('.actionBtn').addClass('tambah');
        $('.modal-title').text('Tambah');
        $('.deleteContent').hide();
        $('.form-horizontal').show();
        $('#id2').val($(this).data('id2'));
        $('#dokumen').val($(this).data('dokumen'));
        $('#myModal2').modal('show');
    });

    $('.modal-footer').on('click', '.tambah', function() {
        $.ajax({
            type: 'post',
            url: '/upload',
            data: {
                '_token': $('input[name=_token]').val(),
                'id':  $('#id2').val(),
                'dokumen': $('#dokumen').val()

            },
            success: function(data) {


            }
        });
    });
});

this is my controller

    public function upload(Request $request)
    {
      $id= $request->get('id');
      $dokumen = $request->file('dokumen');
    var_dump($dokumen);
    }

when i try var_dump($dokumen) always return null can u help me??

0 likes
3 replies
Cheers02's avatar

@tykus i just try formData this is my JS

$(document).ready(function() {

  $(document).on('click', '.tambah-modal', function() {

        $('#footer_action_button2').text("Tambah");
        $('#footer_action_button2').addClass('glyphicon-check');
        $('#footer_action_button2').removeClass('glyphicon-trash');
        $('.actionBtn').addClass('btn-success');
        $('.actionBtn').removeClass('btn-danger');
        $('.actionBtn').addClass('tambah');
        $('.modal-title').text('Tambah');
        $('.deleteContent').hide();
        $('.form-horizontal').show();
        $('#id2').val($(this).data('id2'));
        $('#dokumen').val($(this).data('dokumen'));
        $('#myModal2').modal('show');
    });

    $('.modal-footer').on('click', '.tambah', function() {
        var formData = new FormData($(this)[0]);
        alert(formData);
        $.ajax({
            type: 'post',
            url: '/tambahDokumenSurveyLain',
            data: formData,
            success: function(data) {


            },
            cache: false,
            processData: false
        });
    });
});

but when i try to alert i got this alert [object FormData] and when i try to show the result in controler still got null

tykus's avatar

It's not that easy... the link was for a simple example, not to be copied and pasted

You need to grab the <form> as a jQuery object first since the submit button is not within the actual form element.

$('.modal-footer').on('click', '.tambah', function() {
    var form = $('#tambah-modal form');
    var formData = new FormData(form);
    
    $.ajax({
            type: 'post',
            url: '/tambahDokumenSurveyLain',
            data: formData,
            cache: false,
            processData: false,
        success: function(data) {


            },
        });
});

Please or to participate in this conversation.