I'm using modal to submit new data. It uses ajax to consume API.
I have no problem sending the data through the API, but I'm not good at html, it's confusing how you can pass the value of the form data.
In this case I'm showing a modal for the user to input new data then submit it to an ajax function addData()
and I want to send all form values, but I don't know how to pass the data to the function.
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Tambah Karyawan</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" id="tambah_karyawan">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name_add" autofocus>
<p class="errorTitle text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Role:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="role_add" autofocus>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Email:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="email_add" autofocus>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Password:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="password_add" autofocus>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Foto:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="foto_add" autofocus>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Cabang:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="cabang_add" autofocus>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-success add" onclick="document.getElementById('tambah_karyawan').addData()" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-check'></span> Add
</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
Once I can pass the values to the function, I can do the rest just fine.
Edit: here is the solution for reference
function addData()
{
$.ajax({
type: 'POST',
url: "{{ url('someajaxurl') }}",
dataType: "json",
data: $('#tambah_karyawan').serialize(),
success: function (data) {
console.log(data);
},
error: function (data) {
//
}
});
}
What I learned: I thought you must pass the value directly to addData() something like addData(value1,value2,value3), but in this case you just called addData() and it will pass all values, then catch it in the ajax function.