Aryan Jain
Aryan Jain

Reputation: 1

How can I pass textarea data in below code?

I am submitting my form and in my form I'm sending text-area and textbox data but I didn't get text-area details.

So how can I pass text-area details in below code ?

$(document).on('click','#contactsave',function(e){

        e.preventDefault(); //keeps the form from behaving like a normal (non-ajax) html form
        var $form = $("#contactForm");
        var url = $form.attr('action');
        var formData = {};

        //submit a POST request with the form data
        $form.find('input').each(function(){
            formData[ $(this).attr('name') ] = $(this).val();
        });

        $('.blockui').show();   
        $('.overlayblock').show();
        $.post(url, formData, function(response)
        {
             var obj = $.parseJSON(response);

             if(obj.status == 1){
                $(".succmsg").html(obj.message);
             }else{
                $(".succmsg").html(obj.message);
             }
             $("#description").val(''); $("#emails").val(''); $("#mobile").val(''); $("#name").val('');
             setTimeout( function(){$('.succmsg').html('');} , 4000);
             $('.blockui').hide();  
             $('.overlayblock').hide();
        }).fail(function(response)
        {
            $.each(response['responseJSON'],function(value, index){
                $("#"+value).parent(".form-group").addClass("has-error");
                $("#"+value).siblings(".error").html(index);
            });
            $('.blockui').hide();   
            $('.overlayblock').hide();
        });

    });

Upvotes: 0

Views: 161

Answers (3)

Kevin Krieger
Kevin Krieger

Reputation: 121

You can try this:

$('form').serialize()

With this example:

<form>
 <input type="radio" name="foo" value="1" checked="checked" />
 <input type="radio" name="foo" value="0" />
 <input name="bar" value="xxx" />
 <select name="this">
  <option value="hi" selected="selected">Hi</option>
  <option value="ho">Ho</option>
</form>

this produces: "foo=1&bar=xxx&this=hi"

Upvotes: 0

Mahdi Younesi
Mahdi Younesi

Reputation: 7489

because you are getting only input type each statment

$form.find('input').each(function(){
        formData[ $(this).attr('name') ] = $(this).val();
});

use serialize() function of jquery, then the all elements's values will be sent

var formData = $("#contactForm").serialize();

$.post({

    data: formData ,
    dataType: "json",
    success: function(data) {

    },
    error: function() {
    }
});

Upvotes: 0

Boghani Chirag
Boghani Chirag

Reputation: 225

  $(document).on('click','#contactsave',function(e){

    e.preventDefault(); //keeps the form from behaving like a normal (non-ajax) html form
    var $form = $("#contactForm");
    var url = $form.attr('action');
    var formData = {};

    //submit a POST request with the form data
    $form.find('input,textarea').each(function(){
        formData[ $(this).attr('name') ] = $(this).val();
    });

Or

  var formData = $('#contactForm').serialize();

Try this one

Upvotes: 1

Related Questions