jQuery Ajax File Upload error serialize

Asked

Viewed 165 times

0

I am implementing an upload system in my system but am not able to recover the upload data via jquery serial when I pass the data without input file it works normal and does not present me error.

PHP

<?php


namespace App\Controllers;

use App\Models\newsModel;
use Core\Base\Controller;

class adminNewsController extends Controller
{
    public $newsModel;

    public function __construct()
    {
        parent::__construct();
       
        $this->newsModel = new newsModel();
    }


    public function index()
    {

        $this->renderView("admin/news/index", "layout");
    }

    public function create()
    {
        $this->renderView("admin/news/create", "layout");
    }


    public function ajax()
    {
        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') :
            $Action = filter_input(INPUT_POST, 'acao', FILTER_SANITIZE_STRING);
            switch ($Action):

                case 'form_cad':
                    $post = filter_input_array(INPUT_POST, FILTER_DEFAULT);
                    dump($post);
                    break;

            endswitch;
        endif;
    }

} 

Jquery/Ajax:

  $(selector).on("submit", 'form[name="form_cad"]', function () {

            $.ajax({
                url: '/adm/news/ajax',
                data: new FormData(this),
                contentType: false,
                processData: false,
                type: 'POST',
                success: function (data) {
                    console.log(data);
                }
            });
            return false;

        });

HTML

<form action="" name="form_cad" method="post" enctype="multipart/form-data">
<div class="row">
    <div class="col-12">
        <div class="form-row">
            <label for="">Titulo</label>
            <input name="news_title" class="form-control" type="text" value="sasasasa">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-4">
        <div class="form-row">
            <label for="">Imagem</label>
            <input type="file" name="news_images">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-12">
        <textarea id="editor" name="news_content" class="'text-ckedt">sasaassaas</textarea>
    </div>
</div>
<button name="cadastrar" type="submit"  class="btn btn-3d">CADASTRAR</button>

1 answer

1

You can’t use it filter_input_array() for $_FILES as you can see in manual:

mixed filter_input_array ( int $type [, mixed $definition ] )

type

One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.

One of a few solutions would be to use the filter_var_array($_FILES, $Filters);

Browser other questions tagged

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