user3504605
user3504605

Reputation: 21

Submit a form that contain a textarea using AJAX

I can't submit a form using AJAX , the problem exist just when i include a textarea in the from , when i delete it, the form submit correctly .

$(document).ready(function() {
    $('form').submit(function(event) {
        var formData = {
            'name'              : $('input[name=name]').val(),
            'cont'          : $('input[name=cont]').val(),
            'date'  : $('input[name=date]').val(),
            'prix'              : $('input[name=prix]').val(),
            'timestart'             : $('input[name=timestart]').val(),
            'nbm'   : $('input[name=nbm]').val()             
        };
        $.ajax({
            type        : 'POST', 
            url         : 'zzz.php',
            data        : formData,
            dataType    : 'json' 
        }) ........

Upvotes: 0

Views: 69

Answers (2)

sandipshirsale
sandipshirsale

Reputation: 791

please use jut a single line of code

$("#formid").serialize();

instead of


    var formData = {`enter code here`
                'name'              : $('input[name=name]').val(),
                'cont'          : $('input[name=cont]').val(),
                'date'  : $('input[name=date]').val(),
                'prix'              : $('input[name=prix]').val(),
                'timestart'             : $('input[name=timestart]').val(),
                'nbm'   : $('input[name=nbm]').val()             
            };

and then try to submit

Upvotes: 0

smnh
smnh

Reputation: 1745

If I get it right from your comment your textarea has a name="cont" attribute:

<textarea name="cont" placeholder="your msg"></textarea>

But to get the reference to this textarea element you are using input[name=cont] selector which selects any input element with name="cont" attribute but not a textarea element.

To select the textarea you should use:

$('textarea[name=cont]')

Upvotes: 1

Related Questions