Form:
<form action="" id="register" method="post">
<input type="text" placeholder="eg. John">
<input type="text" placeholder="eg. Appleseed">
<input type="text" placeholder="[email protected]">
</form>
JS:
$('form#register').on('submit',function (e) {
$.ajax({
url: 'submit.php',
cache: false,
type: 'POST',
context: this,
data : $(this).serialize(),
success: function(json) {
console.log("json: " + json);
}
});
e.preventDefault();
});
PHP:
$formData = json_encode($_POST);
echo print_r($formData,1);
... after filling the form and hitting submit, it does submit the form without an error, but the data returned (JSON) is empty:
json: []
What am I doing wrong?
This is because you are not using name
attribute in your fields
serialize()
Requires name
field in your form
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With