samedi 2 mai 2020

Ajax not calling in Laravel 5.8

Web.php file

Route::group(['middleware'=>['auth','admin']], function(){
    Route::get('/admin_addEngagementData', 'Admin\DashboardController@getEngagementForm');
    Route::post('/admin_insertEngagementData', 'Admin\DashboardController@insertEngagementData');
});

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire