0

I was trying to figure it out to submit a comma delimited value on a multiple select. Here's my sample code:

$( 'myform').ajaxForm({
    beforeSubmit: function(data) {
        var queryString = compressParam($.param(data));
        //console.log('params:'+queryString);
    },
    success: function(response){
        $('div').html(response);
    }
    error: function(a,e,et) { alert('ERROR 101: '+et); }
});

function compressParam(data) {
    data = data.replace(/([^&=]+=)([^&]*)(.*?)&\1([^&]*)/g, "$1$2,$4$3");
    return /([^&=]+=).*?&\1/.test(data) ? compressParam(data) : data;
}

I found a function that will combine same parameter. Works like a charm but don't know how to submit as data or do you have other idea how to submit a multiple parameter in comma delimited using ajaxForm?

mrrsb
  • 663
  • 3
  • 13
  • 28
  • Use $post and add parameters into an array. Then pass array as a value of json object. Refer here https://stackoverflow.com/questions/5570747/jquery-posting-json and here https://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – Shiham Jun 07 '18 at 09:22
  • thanks bro for the link. but would it possible using the ajaxForm – mrrsb Jun 07 '18 at 09:24
  • Well $ajax, $post and $get are pure JQuery methods that can be used to submit ajax requests without using any plugins. For ajaxForm plugin you will have to serialize the form to submit as data. refer documentation here http://malsup.com/jquery/form/#api – Shiham Jun 07 '18 at 09:34
  • done with the serialize data but not submitted as comma seperated =) – mrrsb Jun 08 '18 at 00:34

1 Answers1

0

I know this is a late response to my question but hopefully helps anyone just like me looking to solve this problem again and again. Kudos to John Resig for the code and explanation.

function compress(data){
    var q = {}, ret = "";
    data.replace(/([^=&]+)=([^&]*)/g, function(m, key, value){
        q[key] = (q[key] ? q[key] + "," : "") + value;
    });
    for ( var key in q )
        ret = (ret ? ret + "&" : "") + key + "=" + q[key];
    return ret;
}
mrrsb
  • 663
  • 3
  • 13
  • 28