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>