3zzy 3zzy -4 years ago 89
PHP Question

AJAX form submission - No data returned

Form:

<form action="" id="register" method="post">
<input type="text" placeholder="eg. John">
<input type="text" placeholder="eg. Appleseed">
<input type="text" placeholder="youremail@domain.com">
</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?

Answer Source

This is because you are not using name attribute in your fields

serialize() 

Requires name field in your form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download