Skip to main content

Store and update data from ajax with file also using formdata

Script
<script>
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN": jQuery(`meta[name="csrf-token"]`).attr("content")
}
});


$(document).ready(function () {
$('.dropify').dropify();
$("#msform").on('submit', (e) => {
// $("#msform").submit(function() {
e.preventDefault();
// var innovationvalue = new FormData();
// innovationvalue.append('logo', this);
// this.preventDefault();

var form = $('#msform')[0];
var innovationvalue = new FormData(form);

console.log(innovationvalue);
// alert(innovationvalue);
$.ajax({
async: false,
method: 'POST',
// type: 'PUT',
url: 'innovationstore',
data: innovationvalue,
dataType: 'json',
enctype: "multipart/form-data",
processData: false, // Important!
contentType: false,
cache: false,
}).done((result) => {
// alert(result);
$('#loader').css('display', 'none');
console.log("------done------");
console.log(result);

$("#innovationid").val(result);
// location.reload();


}).fail((error) => {
// alert(result);
console.log("------fail------");
$('#loader').css('display', 'none');
console.log(error);
// location.reload();

});
});
});

</script>

<form method="POST" action="" enctype="multipart/form-data" id="msform">
...
input fields
...
</form>


controller
public function innovationstore(Request $request)
{
$input = $request->all();
$logo = '';
if (!empty($request->file('logo'))) {
$file = $request->file('logo');
$path = public_path() . '/InnovationImage';
$name = uniqid() . '_' . $file->getClientOriginalName();
$name = str_replace(' ', '_', $name);
if ($file->move($path, $name)) {
$logo = $name;
$input['logo'] = $logo;
}
}
$projecttitle = $request->projecttitle;
$slug = str_slug($projecttitle, '-');
$input['slug'] = $slug;
$id="";
// return($input);
if($input['id']==null) {
$status = Innovation::create($input);
$id = $status->id;
}
else {
$innovation=Innovation::find($input['id']);
$innovation->problemandcontext=$input['problemandcontext'];
$innovation->innovationbrought=$input['innovationbrought'];
$innovation->marketopportunity=$input['marketopportunity'];
$innovation->impact=$input['impact'];
$innovation->sustainability=$input['sustainability'];
// return ($input);
$status = $innovation->update();
$id = $input['id'];
}
return json_encode($status);
// return redirect('frontend/index');
}







Comments

Popular posts from this blog

laravel file manager sorting by time default

Ref :  https://github.com/UniSharp/laravel-filemanager/issues/337 To load files order by "time DESC" you can change the code in vendor/unisharp/laravel-filemanager/src/traits/LfmHelpers.php public function sortFilesAndDirectories($arr_items, $sort_type) { if ($sort_type == 'time') { $key_to_sort = 'updated'; } elseif ($sort_type == 'alphabetic') { $key_to_sort = 'name'; } else { $key_to_sort = 'updated'; } return strcmp($a->{$key_to_sort}, $b->{$key_to_sort}); }); return $arr_items; } with public function sortFilesAndDirectories($arr_items, $sort_type) { if ($sort_type == 'time') { $key_to_sort = 'updated'; } elseif ($sort_type == 'alphabetic') { $key_to_sort = 'name'; } else { $key_to_sort = 'updated'; ...

Setting and Getting Cookie and Session in laravel

First of all lets discuss many ways to set cookies inside controller : $response = new Response ( redirect ( 'news' )); //$minute=5; // $response->withCookie(cookie('name', 'virat', $minutes)); $response -> withCookie ( cookie ()-> forever ( 'news_id' , $newsid )); return $response ; // or // Cookie::queue('news_id', $news_id, 15); // return redirect('news'); // or // return redirect('news')->withCookie(cookie('name', 'virat', $minutes)); use Illuminate\Support\Facades\ Request ; use Illuminate\Http\ Response ; use Illuminate\Support\Facades\ Cookie ; To get cookies: inside controller $val = Request :: cookie ( 'news_id' ); // dd($val); $v = Cookie :: get ( 'news_id' ); // dd($v); or $value = $request -> cookie ( 'name' ); in blade: {{ Cookie :: get ( 'news_id' ) }} Now lets see for session: Ref...