Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to process a form via ajax request. The problem is that I have an array variable inside the form and when I process it via serialize to be send via ajax it returns this:

email_id%5B%5D=1&email_id%5B%5D=2&test=23

That is the result of .serialize.

I am also using multiple select tags for the array variable.

Why am I getting this error and what should I do to avoid and fix it.

<select name="email_id[]" id="email_id_0" style="width: 350px;margin-right: 5;">
</select>

function setList(str){
    var postDatas = $('#form'+str+'').serialize();
    alert(postDatas);
    // $('#crm-feedback').html('<img src="images/ajax-loader.gif"/>');
    $.ajax({
        url: 'somewhere/file.php',
        type: 'POST',
        data: postDatas,
        success: function(data){
            // $('#crm-feedback').html('Saved!').css('color','green');
            alert("test");
        }

    });
}

Thank you.

share|improve this question
 
Do not use serialize. Use POST –  Your Common Sense Jun 20 at 15:50
 
@YourCommonSense POST will still serialize it. –  Kevin B Jun 20 at 15:51
 
What "error" are you getting? the serialized data you are seeing is correct and valid. –  Kevin B Jun 20 at 15:52
 
I am using post, i am using serialize to get all the data of the form. –  magicianIam Jun 20 at 15:52
 
@KevinB the %5B%5D, since those should be [], since my select tags have a name="email_id[]" –  magicianIam Jun 20 at 15:53
show 4 more comments

2 Answers

try following

var array = document.getElementById("<your array elem>");
        var formdata = new FormData();
        for(var i=0; i< array.length; i++){
            formdata.append("<key>",array[i]);
        }

        $.ajax(
         {
             url: "<url>",
             dataType: "<expected return datatype>",
             type: "POST",

             data: formdata,
             success: function (result, status) {
                //process success
             },
             error: function (result, status) {
                //process failure
             }
         });
share|improve this answer
add comment

I have been having a virtually identical problem today which is now solved thanks to help on here - read this post and look at the jsfiddles

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.