Submit form input array with jQuery ajax post

Ok, I want to submit a form via jquery ajax. All inputs are in an array and are multidimensional.

Its a dynamic form using the array key as the question id. This section is used to group questions in a given 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 this is my html example. Here's my jquery example:

$("#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;
            }); 

      

The problem is I am getting this as an array returned:

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

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

)

      

But I need an array like this:

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

      

I'm sure this is simple, but I'm missing something. I know why it does it, but I cannot get it the way I want it. Can anyone please help?

+3


source to share


4 answers


Try below snippet, I haven't tested it but it should probably work.

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

below with snippet

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

      



Try this, it will solve the data coming out before the problems

I developed this

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

      

+3


source


I don't know how to do this in jquery, but you can convert this array to php



$result = array();
foreach($array as $item) {
   $index = intval(preg_replace("/data\[([0-9]*)\]/", '\1' $item['name']));
   $result[$index] = $item['value'];
}

      

0


source


try it

var data = {}; 
$('input[name^="data\\["]').serializeArray().map(function(n){
    var name = n['name'].replace(/data\[([0-9]*)\]\[(.*)\]/, '$1');
    data[name] = n['value'];
});

      

0


source


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

      

gives console error ->

Uncaught SyntaxError: missing) after argument list

0


source







All Articles