Laravel 6 – getting first id in database with ajax

  ajax, jquery, laravel

I’m beginner in jQuery/Ajax. I’m trying to insert data to database with Ajax. I can insert data whatever I want without reload page but when I look at the data with ‘console.log’ I added, I see the id of the first data in the table. Where is my fault?

My blade is:

    <a href="javascript:void(0)" type="button" class="btn btn-success" data-toggle="modal" data-target="#addDoctor" >Add doctor</a>

<div class="modal fade" id="addDoctor" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Add</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <form id="doctorForm" method="POST" action="{{route('add_doctorPost')}}" novalidate>
                @csrf
                <div class="mb-3">
                    <input type="text" class="form-control" name="name" id="name" required>
                </div>
                <div class="mb-3">
                    <label>E-mail </label>
                    <input type="email" class="form-control" name="email" id="email" required>
                </div>
                
               
                <button type="submit"  class="btn btn-warning btn-lg btn-block">Kaydet</button>
            </form>
        </div>
    </div>
    </div>
</div>

My ajax is:

$('#doctorForm').submit(function(e){
    e.preventDefault();

    let name = $("#name").val();
    let email = $("#email").val();

    $.ajax({
        url: "{{route('add_doctorPost')}}",
        type:"POST",
        data:{name:name,email:email,_token:'{{ csrf_token() }}'},
        success:function(response){
            if(response){
                $("#doctorTable tbody").prepend('<tr><td><p class="c_name">'+ response.name +'</p></td><td><p class="c_name">'+ response.email +'</p></td></tr>')
                $('#doctorForm')[0].reset();
                $('#addDoctor').modal('hide');
                swal.fire("nice!", response.message, "success");
            }
        }
    });


});

Source: Laravel

Leave a Reply