Submit form input array with jquery ajax post

20,361

Try below snippet,I have not tested this but probably it should work.

Replace var data = $('input[name^="data\\["]').serializeArray(); part with below snippet

var data = {};
$.each($('input[name^="data\\["]')​.serializeArray()​, function() {
    data[this.name] = this.value;
})​;

Try this it will solve the data coming in front issues

i have worked out this one

var data = {};
$.each($('select[name^="data\\["] , input[name^="data\\["]').serializeArray(), function() {
   var vv = this.name.replace(/data/, '' ).replace(/(\[[0-9]\])$/,'');
   data[vv] = this.value;           
});
Share:
20,361
Alan
Author by

Alan

Updated on October 24, 2020

Comments

  • Alan
    Alan over 3 years

    Alright I want to submit a form thru jquery ajax. All the inputs are in an array and it is multidimensional.

    Its a dynamic form that uses the array key as the question id. The subkey is used for grouping the questions at a question set.

    <form name="testing" id="testing" method="post">
        <label>Question 1?</label> 
        <input type="text" name="data[14][1]" id="" class="" value=""><br>
        <label>Question 2?</label> 
        <input type="text" name="data[16][1]" id="" class="" value=""><br>
        <label>Question 1?</label> 
        <input type="text" name="data[14][2]" id="" class="" value=""><br>
        <label>Question 2?</label> 
        <input type="text" name="data[16][2]" id="" class="" value=""><br>
        <label>Question 3?</label> 
        <select name="data[19]" id="" class="">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select><br>
        <input type="submit" value="Submit">
    </form>
    

    So that is my example html. Here is my example jquery:

    $("#testing").submit(function() { 
               var data = $('input[name^="data\\["]').serializeArray();
                    $.ajax({ 
                     type: "POST",
                     url:  "upload.php",
                     data: {internalform: "submit", data: data},
                     dataType : "text",
    
               success: function(returndata){
                if(returndata == "no") 
                 { return false;
                 } else {
                   alert("clicked 1 " + returndata);
                    }
                     } 
                     });    
                return false;
                }); 
    

    Problem is I get this as a return array:

    Array
    (
        [0] => Array
            (
                [name] => data[14]
                [value] => sd
            )
    
        [1] => Array
            (
                [name] => data[16]
                [value] => s
            )
    
    )
    

    But I want an array like this:

    Array ( [14] => ddd [16] => ddd [19] => 4 ) 
    

    Im sure its simple but I'm missing something. I know why its doing it but I can't get it the way I want it/need it. Can someone help?

  • Alan
    Alan over 11 years
    Its close. I get this as a response. ( [data[14] => ssssss [data[16] => ss ) Two issues with that. 1. the [ in front of data isn't suppose to be there. 2. the select input isn't getting posted. Almost there though.
  • Alan
    Alan over 11 years
    Thanks, I messed around with it and I made it work with this: var data = {}; $.each($(this).serializeArray(), function() { data[this.name] = this.value; }); That did what I wanted and I removed the data part from the name in the form so the form name is only the question id