Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending multidimensional array to form data

Ok I'm trying to append data to an existing form when someone submits it.

function submitCustOpts() {
var custoptsids=new Array(".implode(",",$optids).");
var pfrm=document.forms['promotion".$promo_data["promo_id"]."'];

for(var i in custoptsids) {
    selectedVal = $('#'+custoptsids[i]).val();

    var input = $('<input>').attr(
    {
        type: 'hidden',
        name: 'cf_'+custoptsids[i],
        value: selectedVal
    }).appendTo(pfrm);
}
pfrm.submit();
}

EVERYTHING works. So just ignore the first half. The part I can not get it the appending to the form. The custoptsids[i] holds the id and the selectedVal holds the value. It's all working dandy, but I need to put this in a multidimensional array. Right now it submits like this:

<!-- REQUEST: Array
(
    [promo_id] => 164792
    [station_id] => 2478
    [lang] => en
    [cf_28] => 55
    [cf_29] => 61
    [PHPSESSID] => 375ee178f5de3blahblahblah
)
-->

When it should look like this:

<!-- REQUEST: Array
(
    [promo_id] => 164792
    [station_id] => 2478
    [lang] => en
    [cf] => Array
        (
            [28] => 55
            [29] => 60
        )

    [PHPSESSID] => 375ee178f5de3blahblahblah
)
-->

So my question is how do I put those values into the cf array.. cf_28 should be just cf with 28 and 29 as the array keys for the inner array..

like image 602
Peanut Avatar asked Jan 26 '26 21:01

Peanut


1 Answers

I'm happy to help, also if you were really a step from the "solution". The solution would be "pause for a while", "have a coffee/tea", go back to code. But anyway:

var input = $('<input>').attr(
{
    type: 'hidden',
    // name: 'cf_'+custoptsids[i], <- look twice :)
    name: 'cf['+custoptsids[i]+']',
    value: selectedVal
}).appendTo(pfrm);

Its the same for me. EVERY time. Glad I could help.

like image 136
sixFingers Avatar answered Jan 28 '26 14:01

sixFingers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!