ajax serialize code example
Example 1: serialize and send form data jquery ajax
$.ajax({
type : 'POST',
url : 'url',
data : $('#form').serialize()
}
Example 2: jquery serialize
var datastring = $("#contactForm").serialize();
$.ajax({
type: "POST",
url: "your url.php",
data: datastring,
dataType: "json",
success: function(data) {
},
error: function() {
alert('error handling here');
}
});
Example 3: how to use serialize in ajax
$.ajax({
type : 'POST',
url : 'url',
data : {
$('#form').serialize(),
par1 : 1,
par2 : '2',
par3: 232
}
}
Example 4: get serialize data javascript
<form id="myForm" name="myForm">
<div>
<label for="username">Enter name:</label>
<input type="text" id="username" name="username">
</div>
<button type="button" onclick="clickMe()">Click</button>
</form>
function clickMe(){
let myForm = document.getElementById('myForm');
let formData = new FormData(myForm);
console.log(formData.get('username'));
}