Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4623244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:57:33+00:00 2026-05-22T02:57:33+00:00

I am using jQuery, and I would like to upload files with Ajax. I

  • 0

I am using jQuery, and I would like to upload files with Ajax. I have made some searches and found it is not possible.

However, there is one jQuery plugin, jQuery Form Plugin, which allows us to upload files through Ajax.

It works very well, but I have a special problem. Here is my code:

$('#question-form').submit(function() {
    var serialAnswers = '';

    // Create a query string given some fields
    // Format of the query string : answers[0][fr_fr][0]=a1fr&answers[0][fr_fr][1]=2&answers[0][en_uk][0]=a1en&answers[0][en_uk][1]=6&...
    $('#question-answers > div').each(function(idx, elt) {
        $('div[lang]', $(elt)).each(function(idxLang, eltLang) {
            var lang = $(this).attr('lang');
            serialAnswers += 'answers[' + idx + '][' + lang + '][0]=' + $("[answerpart=display]", $(eltLang)).val();
            serialAnswers += '&answers[' + idx + '][' + lang + '][1]=' + $("[answerpart=value]", $(eltLang)).val() + '&';
        });
    });

    $(this).ajaxSubmit({
        datatype: "html",
        type: "POST",
        data: serialAnswers,
        url: $(this).attr("action"),
        success: function(retour) {
            $('#res-ajax').html(retour);
        }
    });

    return false;
});

As you can see, I have to replace the $.ajax call by a $(this).ajaxSubmit() call, with the same options. Moreover, I have to create a query string (serialAnswers in the code) according to some fields in order to transmit it to the PHP code.

Here is what I used to do when I had no file to upload. I just serialized the form fields and added my query string named serialAnswers:

$.ajax({
    datatype: "html",
    type: "POST",
    data: $(this).serialize() + '&' + serialAnswers,
    url: $(this).attr("action")
    success: function(retour) {
        $("#res-ajax").html(retour);
    }
});

But my problem is that the form plugin transmits my additional data (the query string) that way (in the PHP file):

Array
(
    [question_heading_fr_fr] => something
    [question_heading_en_uk] => nothing
    [question_type] => 5
    [0] => a
    [1] => n
    [2] => s
    [3] => w
    [4] => e
    [5] => r
    [6] => s
    [7] => [
    [8] => 0
    [9] => ]
    [10] => [
    [11] => f
    [12] => r
    [13] => _
    [14] => f
    [15] => r
    [16] => ]
    ....
)

According to the documentation, I have to pass a JSON object to the data option, like this:

data: { key1: 'value1', key2: 'value2' }

But I don’t know how to convert my query string to a JSON object and if it would be interpreted as an array on the PHP side.

Is there a solution?

EDIT: Even if I use an iframe, I don’t know how to add a query string with information which does not come from the form (my serialAnswer from the code above).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:57:33+00:00Added an answer on May 22, 2026 at 2:57 am

    EDIT: I found a new solution, and it works 🙂

    I have modified the jQuery Form Plugin. Plugin page: http://jquery.malsup.com/form/#getting-started. Script: https://github.com/malsup/form/raw/master/jquery.form.js

    In order to use this syntax:

    $('#myform').submit(function() {
        var queryString = 'answers[0][fr_fr][0]=a1fr&answers[0][fr_fr][1]=2&answers[0][en_uk][0]=a1en&answers[0][en_uk][1]=6';
    
        $(this).ajaxSubmit({
            dataType:  'html',
            data: queryString
        });
    
        return false; 
    });
    

    instead of:

    $('#myform').submit(function() {
        $(this).ajaxSubmit({
            dataType:  'html',
            data: {'key':'value' }
        });
    
        return false; 
    });
    

    I have modified the plugin code.

    Find this:

    var n,v,a = this.formToArray(options.semantic);
    if (options.data) {
        options.extraData = options.data;
        for (n in options.data) {
            if(options.data[n] instanceof Array) {
                for (var k in options.data[n]) {
                    a.push( { name: n, value: options.data[n][k] } );
                }
            }
            else {
                v = options.data[n];
                v = $.isFunction(v) ? v() : v; // if value is fn, invoke it
                a.push( { name: n, value: v } );
            }
        }
    }
    

    And delete all the if (options.data) and its content. A few lines after these ones, find this:

    var q = $.param(a);
    

    And add this right after it:

    if (options.data) {
        options.extraData = options.data;
        q += '&' + options.data;
    }
    

    In the function fileUpload(), find this:

    var extraInputs = [];
    try {
        if (s.extraData) {
            for (var n in s.extraData) {
                extraInputs.push(
                    $('<input type="hidden" name="'+n+'" value="'+s.extraData[n]+'" />')
                        .appendTo(form)[0]);
            }
        }
    
        // Add iframe to doc and submit the form
        $io.appendTo('body');
        io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
        form.submit();
    }
    

    And replace it by:

    var extraInputs = [];
    try {
        if (s.extraData) {
            var couples = s.extraData.split('&');
            for (var c=0 ; c < couples.length ; c++)
            {
                var couple = couples[c].split('=');
                extraInputs.push(
                    $('<input type="hidden" name="'+couple[0]+'" value="'+couple[1]+'" />')
                        .appendTo(form)[0]);
            }
        }
    
        // add iframe to doc and submit the form
        $io.appendTo('body');
        io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
        form.submit();
    }
    

    You now can add a query string thanks to these modifications and uploading an image at the same time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to upload a file using JQuery-File-Upload, but using HTTP PUT instead
I would like to have dockable panels using jquery that behave much like the
I am new to using jquery and would like to know how to append
I am relatively new to using jQuery and would like to use the load
I'm using jQuery Validate and would like to re-validate a group of fields whenever
I would like to manipulate the HTML inside an iframe using jQuery. I thought
I would like to print the selected values of all selects using jQuery. I
I'm using jQuery with the validators plugin. I would like to replace the required
I'm using jQuery validation in ASP.net MVC. I would like to show validation summary
Using xVal with Jquery validation and would like the error messages to be shown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.