Search code examples
ajaxblobform-data

Php FormData is null


I have a form with id: formC, on submit i call ajax:

        var datiForm = new FormData();

        var pos = [];
        var i = 0;
        posizioni.each(function () {

            if($(this).find("input[type=checkbox]").is(":checked")){

                pos[i] = $(this).find("input[type=checkbox]").data("id");
                i++;
            }


        });

        datiForm.append("nome",nome.val());
        datiForm.append("cognome",cognome.val());
        datiForm.append("email",email.val());
        datiForm.append("telefono",telefono.val());
        datiForm.append("dataNascita",dataNascita.val());
        datiForm.append("titolo",titolo.val());
        datiForm.append("ruolo",ruolo.find(":selected").data("ruolo"));
        datiForm.append("sede",sede.find(":selected").data("sede"));
        datiForm.append("posizione",pos);
        datiForm.append("cvFile",$("#cvFile")[0].files[0]);

        $.ajax({

            type: "POST",
            data: {datiForm: datiForm},
            url: "saveCandidate.php",
            processData: false,
            contentType: false,
            success: function (data) {

                console.log(data);


            },
            error: function (data) {

                var position = data;


            }


        });

I have a problem, on server $datiForm = $_POST["datiForm"]; is null why? Moreover i have input file where i can select file pdf. I put it in FormData:

datiForm.append("cvFile",$("#cvFile")[0].files[0]);

Now on server i want to take file from $datiForm and save it into mysql as Blob is possible?


Solution

  • You specified the data field incorrectly, it should be just the form data object

    data: datiForm,
    

    also the way you add posizione is not going to work, each entry in yrh array has to be added individually

        posizioni.each(function () {
    
            if($(this).find("input[type=checkbox]").is(":checked")){
    
                datiForm.append("posizione["+i+"]", $(this).find("input[type=checkbox]").data("id"));
                i++;
            }
    
        });
    

    Now on server i want to take file from $datiForm and save it into mysql as Blob is possible?

    Yes