Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="[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?

like image 379
eozzy Avatar asked Jul 03 '15 09:07

eozzy


1 Answers

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

serialize() 

Requires name field in your form

like image 86
PHP Worm... Avatar answered Sep 18 '22 15:09

PHP Worm...