简体   繁体   中英

How to send a variable with formData using jquery ajax

How can I send a variable with formdata? I am sure it is really simple but I've tried some things and it is not working. I am using the blueimp fileupload plugin by the way.

My code now:

<script>
$(function () {
    'use strict';
    var url = window.location.hostname === 'site.nl/demo/server/php/' ?
                '//site.nl/' : 'demo/server/php/';
    $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        formData: [
          { name: 'custom_dir', value: '/fileupload/<?PHP echo $_GET['bedrijf'] ?>/<?PHP echo $_GET['alias'] ?>/' },
          { name: 'cat_id', value: '<?PHP echo $gtc['id']; ?>'},
          { name: 'name', value: $( "#filename" ).val()},
      ],
      add: function (e, data) {
            data.context = $('<button/>').text('Uploaden starten').addClass('font-15 btn btn-secondary btn-lg waves-effect btnadd fullwidth')
                .appendTo('.uploadbutton')
                .click(function () {
                    data.submit();
                });
        },
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('#files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>

This is the part that is empty:

{ name: 'name', value: $( "#filename" ).val()},

The echoed php values work fine, but this one is empty.

I've also tried it this way:

var filename = $( "#filename" ).val();

And then below

{ name: 'name', value: filename},

This is my input field:

<input class="form-control" id="filename" type="text" name="filename" value="">

What am I doing wrong here?

在此处输入图片说明

try:

<input class="form-control" id="filename" type="text" name="filename" value="">

var formData = new FormData(document.getElementById('filename'));

send image,audio,file or text together;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM