0

I'm using Yii2 framework but in this specific situation I didn't use the activeform, so I'm using a normal input file like

<form id="formId">
   <input type="text" name="txt" id="txt" />
   <input type="file" name="nameFile" id="nameFile" />
   <button type="submit">
</form>

in javascript, inside $(document).ready, I have

    $("#formId").on( "submit", function( event ) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            dataType: "json",
            type: "POST",
            data: formData,
            url: "index.php?r=myControllerAction",
            success: function (data) {
                console.log(data);
            },
            error: function (jqXhr, textStatus, errorThrown) {
                console.log(jqXhr);
            }
        });
});

In my controller, the $_FILES is empty. Someone knows why?

1
  • try adding this atrribute: enctype="multipart/form-data" to the form Commented Jan 31, 2020 at 11:04

1 Answer 1

-1

In your form encryption type is missing :

<form id="formId" method="post" enctype="multipart/form-data">
   <input type="text" name="txt" id="txt" />
   <input type="file" name="nameFile" id="nameFile" />
   <button type="submit">
</form>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.