Laravel Form Submit POST method Ajax



<div class="modal-body">
    <form class="" id="room-form">
        Username: <input type="text" name="username" id="username"/>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('general.cancel') }}</button>
    <button type="submit" form="room-form" class="btn btn-primary" id="btn-save">{{ __('general.save') }}</button>
</div>
<script type="text/javascript">
$('#room-form').on('submit', function(e) {
    e.stopPropagation();
    e.preventDefault();
    let form = $(this),
    formId = $(this).attr('id'),
    button = e.originalEvent.submitter,
    formData = $('#' + formId).serialize();
    let url = "{{ route('crematorium.store') }}";
    $.ajax({
        url: url,
        type: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        dataType: 'json',
        data: formData,
        beforeSend : function(){
        },
        done: function(){
            /* AFTER SUBMIT COMPLETE ENABLE SUBMIT BUTTON */
        },
        success: function(resp) {
            if(resp.success){
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if(jqXHR.status == 422){
            }
        }
    })
});
</script>
-------------------OR--------------------------------
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <meta name="csrf-token" content="{{ csrf_token() }}" />
        <title></title>
    </head>
    <body>
        
    </body>
</html>
<script type="text/javascript">
var _token = "{{ @csrf_token() }}";
$.ajax({
    url: url,
    headers: {'x-csrf-token': _token},
    type: 'POST',
    data: { id: recId, status: new_status}
})
.done(function(resp) {
    if(resp.success){
        //Todo
    } 
});
</script>

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment