$.ajax({
type: "POST",
url: "ajaxform.php", // url that the data need to go
data: $("form").serialize(),
success: function(data) {
if (data) {
alert("your first name is: " + $('input[name=FirstName]').val() + " Last name is: " + $('input[name=LastName]').val())
}
}
});
try this if you want to use values in the data object then it will depend on the object. Also you need to put the submit buton inside the form as well
$(document).ready(function(){
$("form").submit(function(){
alert("your first name is: " + $('input[name=FirstName]').val() + " Last name is: " + $('input[name=LastName]').val())// remove this alert when ajax code is working. it will not work here in the fiddle
$.ajax({
type: "POST",
url: "ajaxform.php", // url that the data need to go
data: $("form").serialize(),
success: function(data) {
if (data) {
alert("your first name is: " + $('input[name=FirstName]').val() + " Last name is: " + $('input[name=LastName]').val())
}
}
});
return false;
});
});
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<form id="form" method="post" action="">
First name: <input type="text" name="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit">
</form>
</body>
</html>