https://drive.google.com/file/d/1kuyeT3LA22IuN3o_kypOyXesEXMLv31e/view?usp=sharing
Sunday, June 7, 2020
Form Submit using Ajax + Laravel with File Upload
HTML
-----------------------------------------
<!--begin::Entry-->
<div class="main-content p-t-b-50 pt-155">
<!--begin::Container-->
<div class="container">
<!--begin::Card-->
<div class="card card-custom gutter-b">
<div class="card-header">
<h3 class="card-title">Capsule Create</h3>
</div>
<!--begin::Form-->
<form onsubmit="return false;" id="capsule_frm">
@csrf
<div class="card-body">
<div class="row">
<div class="col-md-6">
<input type="hidden" class="form-control" name='id' id="moduleId" value="{{$data->id ?? ''}}">
<div class="form-group">
<label for="nominee">Nominee</label>
<input type="text" class="form-control" name='nominee' id="nominee" placeholder="Enter Nominee Emain Address" value="{{$data->nominee ?? ''}}" >
<div class="error text-danger" id='nominee_error'></div>
</div>
<div class="form-group">
<label for="release_on">Release On</label>
<select class="form-control" name='release_on' id="release_on">
<option value="die" {{(!empty($data->release_on) && $data->release_on == 'die') ? 'selectd' : ''}}>When I die</option>
<option value="future_date" {{(!empty($data->release_on) && $data->release_on == 'future_date') ? 'selected="selected"' : ''}}>Future Date</option>
</select>
<div class="error text-danger" id='release_on_error'></div>
</div>
<div class="form-group" id="release_date-box">
<label for="release_date">Release Date</label>
<input type="date" disabled class="form-control datepicker" data-formet="dd-mm-yy" name='release_date' id="release_date" placeholder="Enter Release Date" value="{{!empty($data->release_date) ? date('Y-m-d', strtotime($data->release_date)) : ''}}" >
<div class="error text-danger" id='release_date_error'></div>
</div>
<div class="form-group">
<label for="expire_date">Expire Date</label>
<input type="date" class="form-control datepicker" data-formet="dd-mm-yy" name='expire_date' id="expire_date" placeholder="Enter Release Date" value="{{!empty($data->expire_on) ? date('Y-m-d', strtotime($data->expire_on)) : ''}}" >
<div class="error text-danger" id='expire_date_error'></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="capsule_type">Capsule Type</label>
@if(!empty($data->id) && !empty($data->capsule_type_id))
<!-- <input type="hidden" name="capsule_type" value="{{$data->capsule_type_id}}"> -->
@endif
<select class="form-control" name='capsule_type' id="capsule_type" {{!empty($data->id) ? "disabled=disabled" : ""}} >
@foreach($capsuleType as $ct)
<option value="{{$ct->id}}" data-slug="{{ $ct->slug ?? ''}}" {{(!empty($data->capsule_type_id) && $ct->id == $data->capsule_type_id) ? 'selected' : ''}}>{{$ct->name}}</option>
@endforeach
</select>
<div class="error text-danger" id='capsule_type_error'></div>
</div>
<div class="form-group" id="video-box">
@if(!isset($data))
<label for="video">Upload Video Capsule</label>
<input type="file" disabled name="video" id="video">
<input type="hidden" name="old_video" id="old_video" value="{{$data->video ?? ''}}">
@endif
<a href="{{$data->video ?? ''}}">{{$data->video ?? ''}}</a>
<div class="error text-danger" id='video_error'></div>
</div>
<div class="form-group" id="audio-box">
@if(!isset($data))
<label for="audio">Upload Audio Capsule</label>
<input type="file" disabled name="audio" id="audio">
<input type="hidden" name="old_audio" id="old_audio" value="{{$data->audio ?? ''}}">
@endif
<a href="{{$data->audio ?? ''}}">{{$data->audio ?? ''}}</a>
<div class="error text-danger" id='audio_error'></div>
</div>
<div class="form-group">
<label for="text">Text</label>
<textarea class="form-control" name='text' id="text" placeholder="Enter text">{{ $data->text ?? ""}}</textarea>
<div class="error text-danger" id='text_error'></div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<input type="button" id="submit_info" class="btn btn-primary" value="Save"/>
<a type="button" id="back_btn" class="btn btn-default" href="{{route('client.capsule')}}" value="Back">Back</a>
<span class="submit_notification">
@if (Session::has('message'))
<div class="text-{{ Session::get('status') }}">{{ Session::get('message') }}</div>
@endif
</span>
</div>
</form>
<!--end::Form-->
</div>
</div>
</div>
Javascript
----------------------------------------------------------
<script type="text/javascript">
var getUser = "{{ route('user_drop') }}";
var getCapsuleType = "{{ route('capsule_type_drop') }}";
var saveUrl = "{{ (!isset($data)?url('capsule/save'):url('capsule/update')) }}";
var returnUrl = "{{ url('capsule') }}";
var capsuleType = "{{!empty($data->capsule_type_id) ? $data->capsule_type_id : 1}}";
var releaseOn = "{{!empty($data->release_on) ? $data->release_on : 'die'}}";
function checkCapsuleType(type){
if(type == 2) {
$('#video-box').show();
$('#audio-box').hide();
$('#video-box').find('#video').removeAttr('disabled');
$('#audio-box').find('#audio').attr('disabled', 'disabled');
} else if(type == 3) {
$('#audio-box').show();
$('#video-box').hide();
$('#audio-box').find('#audio').removeAttr('disabled');
$('#video-box').find('#video').attr('disabled', 'disabled');
} else {
$('#video-box,#audio-box').hide();
$('#audio').find('#audio').attr('disabled','disabled');
$('#video').find('#video').attr('disabled', 'disabled');
}
}
function checkReleaseOn(releaseOn){
if(releaseOn == 'future_date') {
$('#release_date-box').show();
$('#release_date').removeAttr('disabled');
} else {
$('#release_date-box').hide();
$('#release_date').attr('disabled', 'disabled');
}
}
$(document).ready(function() {
$('#video-box,#audio-box').hide();
checkCapsuleType(capsuleType);
checkReleaseOn(capsuleType);
$('#capsule_type').change(function(){
// var type = $('#capsule_type').find(":selected").data('slug');
var type = $(this).val();
checkCapsuleType(type);
});
$('#release_date-box').hide();
checkReleaseOn(releaseOn);
$('#release_on').change(function(){
var releaseOn = $('#release_on').val();
checkReleaseOn(releaseOn);
});
$(document).off('click', '#submit_info').on('click', '#submit_info', function() {
var btn = $("#submit_info");
var moduleId = $("#moduleId").val();
var old_val = btn.val();
var form = btn.closest('form');
var formData = new FormData(form[0]);
$.ajax({
url: saveUrl,
type: 'post',
dataType: 'json',
// data: form.serialize(),
data: formData,
contentType: false,
cache: false,
processData:false,
beforeSend: function() {
$('.error,.submit_notification').html('');
form.find(".form-control").removeClass("red-border");
$('.btn').attr("disabled", "disabled").val("Sending...");
},
success: function(result) {
$('.btn').removeAttr("disabled").val(old_val);
form.find(':input:enabled:visible:first').focus();
if (result.status == 'validation') {
$.each(result, function(i, val) {
if (val != "") {
form.find("#" + i + "_error").text(val);
}
});
$("body").animate({
scrollTop: 0
}, "slow");
} else if (result.status === 'success') {
if (moduleId) {
window.location.href = returnUrl;
} else {
form[0].reset();
$('.submit_notification').html('<span class="text-success error">' + result.message + '</span>');
$('#user').val('').trigger('change');
}
} else {
$('.error').html('');
$('.submit_notification').html('<span class="text-danger error">' + result.message + '</span>');
}
},
error: function(e) {
$('.btn').removeAttr("disabled").val(old_val);
$('.submit_notification').html('<span class="text-danger error">Something Went Wrong!... Please try again after refresh</span>');
}
});
});
});
</script>
PHP
-------------------------------------
public function store(Request $request)
{
$input = $request->all();
$validator = Validator::make($input, [
'capsule_type' => 'required_if:id,!=,""',
'nominee' => 'required',
'text' => 'required',
'video' => 'required_if:capsule_type,==,2|mimetypes:video/avi,video/mp4,video/3gpp,video/mpeg,video/quicktime|file|max:4000',
'audio' => 'required_if:capsule_type,==,3|mimetypes:audio/mp3,audio/mpeg|file|max:4000',
]);
if ($validator->fails()) {
$result['status'] = 'validation';
$result['capsule_type'] = $validator->errors()->first('capsule_type');
$result['nominee'] = $validator->errors()->first('nominee');
$result['text'] = $validator->errors()->first('text');
$result['video'] = $validator->errors()->first('video');
$result['audio'] = $validator->errors()->first('audio');
} else {
if (isset($input['id']) && !empty($input['id'])) {
$capsule = Capsule::find($input['id']);
$msg = "Updated";
} else {
$capsule = new Capsule;
$msg = "Created";
$capsule->capsule_type_id = isset($input['capsule_type']) ? $input['capsule_type'] : "";
}
$video = $request->file('video');
$audio = $request->file('audio');
$user_id =auth('client')->user()->id;
//Move Uploaded audio
if (!empty($audio)) {
$audioName = str_slug($audio->getClientOriginalName(), "-") . '_' . $user_id . '_' . time() . "." . $audio->getClientOriginalExtension();
$destinationPath = public_path('upload/audio');
$audio->move($destinationPath, $audioName);
$capsule->audio = $audioName;
}
// else {
// $audioName = $input['old_audio'];
// }
//Move Uploaded video
if (!empty($video)) {
$videoName = str_slug($video->getClientOriginalName(), "-") . '_' . $user_id . '_' . time() . "." . $video->getClientOriginalExtension();
$destinationPath = public_path('upload/video');
$video->move($destinationPath, $videoName);
$capsule->video = $videoName;
}
// else {
// $videoName = $input['old_video'];
// }
// $theOtherKey = env('APP_KEY');
// $text = $input['text'];
// $encriptText = Crypt::encryptString($text);
// $encriptText = Crypt::encryptString($encriptText);
// $decriptText = Crypt::decryptString($encriptText);
// $decriptText = Crypt::decryptString($decriptText);
// print_r($encriptText);
// echo "<br>";
// print_r($decriptText);
// exit;
$capsule->user_id = $user_id;
$capsule->nominee = isset($input['nominee']) ? $input['nominee'] : "";
$capsule->text = isset($input['text']) ? $input['text'] : "";
$capsule->release_on = isset($input['release_on']) ? $input['release_on'] : "die";
$capsule->release_date = !empty($input['release_date']) ? date('Y-m-d', strtotime($input['release_date'])) : null;
$capsule->expire_on = !empty($input['expire_date']) ? date('Y-m-d', strtotime($input['expire_date'])) : null;
$capsule->revision = ($capsule->revision + 1);
$capsule->admin_key = rand();
$capsule->client_key = rand();
if ($capsule->save()) {
$result['status'] = 'success';
$result['message'] = 'Capsule ' . $msg . ' successfully';
} else {
$result['status'] = 'error';
$result['message'] = 'error';
}
}
echo json_encode($result);
}
Subscribe to:
Posts (Atom)
Product Category Demo in laravel
https://drive.google.com/file/d/1kuyeT3LA22IuN3o_kypOyXesEXMLv31e/view?usp=sharing