wawan Setiyawan wawan Setiyawan - 1 month ago 5
Ajax Question

getting form to ajax with submit button outside form

i have multiple form, so i create a form with button submit outside form, this form



$("#buttonSubmit").click(function (event) {
event.preventDefault();
if (confirm("Anda yakin akan Checkout ?")) {
var formData = new FormData("form#formData");
$(".loader").show();
$.ajax({
url: 'belanja/belanja_crud.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
dataType: 'json',
success: function (data) {
//console.log(data);
}
});
}
return false;
});

<form id='formData'>
<!-- input bla bla bla -->
</form>
<button id='buttonSubmit' type='button'>Submit</button>





how to get all input from form on above and submit with ajax ?

Answer

To get all data in the form in one instruction you can use .serialize()

E.g

$.ajax({
    type: "POST",
    url: 'belanja/belanja_crud.php',
    data: $("#formData").serialize()
})
.done(function (data) {
    //do somthing
})
.fail(function (xhr, ajaxOptions, thrownError) {
    //do something
});

Or you can see this topic if you want it to JSON Format