Postingan lainnya
tidak bisa menyimpan data ke database menggunakan ajax di laravel
halo gan, ane ada stuck nih, form ane ketika diklik tombol submit nya engga terjadi apa2, dan tidak menyimpan isi dari form yang di isi ke database ini Code nya
ini Index.blade.php nya
<div class="container-fluid">
<div class="row">
<div class="col-md-12 box">
<div class="card">
<div class="card-header">
<h4>Data Karyawan
<button type="button" id="tambahdata" class="btn btn-primary float-right">Tambah Data</button>
</h4>
</div>
<div class="card-body">
<table id="stafftable" class="display" style="width:100%">
<thead>
<tr>
<th width="30">No</th>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
@include('staff.addstaff')
</div>
@endsection
@section('script')
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.10.2/validator.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function() {
$('#stafftable').DataTable({
processing: true,
serverSide: true,
ajax :"{{ route('api.contact') }}",
columns : [
{data : 'id', name: 'id'},
{data : 'name', name: 'name'},
{data : 'email', name: 'email'},
{data : 'action', name: 'action', orderable: false, searchable: false}
]
});
$('#tambahdata').click(function() {
save_method = "add";
$('input[name=_method]').val('POST');
$('#staffModal').modal('show');
$('#staffModal form')[0].reset();
$('.modal-title').text('Add Contact');
})
$('#staffModal form').validator().on('submit', function(e) {
if (!e.isDefaultPrevented()){
var id = $('#id').val();
if(save_method == 'add') url = "{{ url('contact') }}";
else url = "{{ url('contact'). '/' }}"+ id;
$.ajax({
url : url,
type: "POST",
data: $('#staffModal form').serialize(),
success: function($data){
$('#staffModel').modal('hide');
},
error:function(){
alert('Oops! Terjadi Kesalahan');
}
});
return false;
}
});
});
ini addstaff.blade.php nya
<div class="modal fade" id="staffModal" tabindex="1" role="dialog" data-backdrop="static" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form action="">
<div method="post" class="form-horizontal" data-toogle="validator">
{{ csrf_field() }} {{ method_field('POST') }}
<h5 class="modal-title">/h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="hidden" name="id" id="id">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" autofocus required>
<span class="help-block with-errors"></span>
</div>
<div class="form-group">
<label for="">Email</label>
<input type="email" name="email" id="email" class="form-control" required>
<span class="help-block with-errors"></span>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary btn-save" id="submit">submit</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
</div>
</form>
</div>
</div>
</div>
ini ContactController@store nya
$data = [
'name' => $request['name'],
'email' => $request['email']
];
return Contact::create($data);
dan ini route nya
Route::resource('contact', 'ContactController');
terima kasih gan sebelum nya, mohon bantuan nya gan
6 Jawaban:
coba cek di bagian console, ada log error ga ketika melakukan action klik?
Jawaban Terpilih
coba diteliti gan, itu methodnya gak didalam element formnya. harusnya <pre> <form action="" method="post"> </form> </pre> semoga berhasil gan ^^
Pertanyaannya menggunakan ajax,tapi sepertinya savenya gak pakai ajax ini. Sepertinya kesalahan Ada di tag form.seperti yg @bosque bilang, method g Ada di dalam tag form. Coba ganti formnya seperti ini
<pre> <form method="post"> </pre>