Ajax not calling in Laravel 5.8

Web.php file

Route::group(['middleware'=>['auth','admin']], function(){
Route::get('/admin_addEngagementData', '[email protected]');
Route::post('/admin_insertEngagementData', '[email protected]');
});

admin_addEngagement.blade.php

@extends('layouts.admin') @section('title') LeapForWord | Content Management @endsection @section('content')
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-primary">
<center>
<h4 class="card-title">Add DataTypes</h4>
</center>
</div>
<div class="card-body">
<div class="table-responsive">
<form method="post" id="dynamic_form">

<span id="result"></span>
<table class="table table-bordered table-striped" id="user_table">
<thead>
<tr>
<th class="text-center">Data</th>
<th class="text-center">Type Of Data</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>

</tbody>
<tfoot>
<tr>
<td colspan="2" align="right">&nbsp;</td>
<td>
<input type="submit" name="save" id="save" class="btn btn-primary" value="Save" />
</td>
</tr>
</tfoot>
</table>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection @section('scripts')
<script>
$(document).ready(function() {

var count = 1;

dynamic_field(count);

function dynamic_field(number) {
html = '<tr>';
html += '<td><input type="text" name="datatype[]" class="form-control" /></td>';
html += '<td><input type="text" name="subdatatype[]" class="form-control" /></td>';
if (number > 1) {
html += '<td><button type="button" name="remove" id="" class="btn btn-danger remove">Remove</button></td></tr>';
$('tbody').append(html);
} else {
html += '<td><button type="button" name="add" id="add" class="btn btn-success">Add</button></td></tr>';
$('tbody').html(html);
}
}

$(document).on('click', '#add', function() {
count++;
dynamic_field(count);
});

$(document).on('click', '.remove', function() {
count--;
$(this).closest("tr").remove();
});

$('#dynamic_form').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '',
method: 'post',
data: $(this).serialize(),
dataType: 'json',
beforeSend: function() {
$('#save').attr('disabled', 'disabled');
},
success: function(data) {
if (data.error) {
var error_html = '';
for (var count = 0; count < data.error.length; count++) {
error_html += '<p>' + data.error[count] + '</p>';
}
$('#result').html('<div class="alert alert-danger">' + error_html + '</div>');
} else {
dynamic_field(1);
$('#result').html('<div class="alert alert-success">' + data.success + '</div>');
}
$('#save').attr('disabled', false);
}
})
});

});
</script>
@endsection

DashboardController.php

class DashboardController extends Controller
{
public function getEngagementForm(){
if(Auth::check()){
return view('admin.admin_addEngagementData');
}
else{
return view('auth.login');
}
}

public function insertEngagementData(Request $request){
if(Auth::check()){
// dd($request);
$id = Auth::user()->id;

if($request->ajax()){
$rules = array(
'datatype.*' => 'required',
'subdatatype.*' => 'required'
);

$error = Validator::make($request->all(), $rules);
if($error->fails()){
return response()->json([
'error' => $error->errors()->all()
]);
}

$datatype = $request->datatype;
$subdatatype = $request->subdatatype;


for($count = 0; $count < count($datatype); $count++){
$data = array(
'user_id' => $id,
'datatype' => $datatype[$count],
'subdatatype' => $subdatatype[$count]
);
$insert_data[] = $data;
}

Lfw_engagement_datatype::insert($insert_data);
return response()->json([
'success' => 'Data Added successfully.'
]);
}
}
else{
return view('auth.login');
}

}
}

I had kept my blade.php file inside view/admin/admin_addEngagement.blade.php file. but I am not getting any output and in console, i am getting error:- jquery.min.js:3049 POST-http://127.0.0.1:8000/admin_insertEngagementData 500 (Internal Server Error)

how can I resolve this issue?

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/2Ys1Wak
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.