Send files and inputs with the same Ajax request

Asked

Viewed 1,636 times

5

I have a code that works perfectly for sending files without refresh using PHP and jQuery and I have another code that works without refresh also but for inserting the data in DB. I can’t put the two codes together.

What I want is basically to send the data and upload a file using the same form and a single request. I believe the problem is in the attribute "date:"

jQuery Code - Send Inputs:

    $( "#create" ).on( "click", function() {
        formId   = $(this).closest( ".form" ).attr("id");
        formArray = $("#" + formId).serializeArray();
        $.ajax({
            type: 'POST', url: 'ajax.php',
            data: { type: 'create', formArray: formArray },
            success: function( msg ) {
                alert( msg );
                window.location.reload();
            },
            error: function() {
                alert('AJAX Error');
            }
        });
        event.preventDefault();
    });

jQuery Code - Upload Files:

    var form;
    $('#fileUpload').change(function (event) {
        form = new FormData();
        form.append('fileUpload', event.target.files[0]);
    });

    $( "#teste" ).on( "click", function() {
        $.ajax({
            type: 'POST', url: 'ajax.php',
            processData: false,
            contentType: false,
            data: form,
            success: function( msg ) {
                alert( msg );
            },
            error: function() {
                alert('AJAX Error');
            }
        });
        event.preventDefault();
    });
  • And which element receives the click? #teste or #create? Or both and must do the same?

  • In fact it would be the only #create, the #test was created only for test purposes same, I wanted to join the two.

1 answer

2


Think of this application as a template and see which one makes the most sense: A registered user with no uploaded file or a uploaded file not associated with any registered user?

If you can see a relationship, you can understand that it makes a lot of sense for the upload routine to occur on callback successful user registration. ;)

If the user’s registration fails, it is a general failure and thus you will not have orphaned files. if the upload fails, at least the user already exists, including primary ID, which may allow future sending(s)).

  • I’ll test it soon, it’s still two requests but it makes sense.

  • It worked perfectly, thanks @Bruno Augusto

  • Glad I could help :)

  • @Brunoaugusto I’m having the same problem, my question is how to make the upload routine in the successful callback? Just call the upload function?

  • This depends on the structure of your application, your development methodology and so on. For example, if you have a pseudo-Javascript class that handles all the details of an upload task, then yes, all you have to do is instantiate the object, pass some arguments and trigger the pseudo-upload method. If you don’t have something like that, it will encode the entire routine within the callback. Again and again and again...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.