views:

55

answers:

1

Hello,

I have a form that currently only parses the first input variable in the POST. I have had to add multiple variables, so I would like the function to loop through and grab all the input parameters and add to the POST

Here's the working code for grabbing the first name and value.... How can I get it to check the form and grab all custom name's and variables.

example here

// dynamically add key/value pair to POST body
function read_custom_param(){
var nameEl = document.getElementById("custom_name");
var valueEl = document.getElementById("custom_value");

// save custom param name and value
var cust_param_name = nameEl.value;
var cust_param_value = valueEl.value;

// remove old custom param form elements
if (valueEl.parentNode && valueEl.parentNode.removeChild){
         valueEl.parentNode.removeChild(valueEl);
}
if (nameEl.parentNode && nameEl.parentNode.removeChild){
         nameEl.parentNode.removeChild(nameEl);
}

// add new custom param form elements
var el=document.createElement("input");
el.type="text";
el.name=cust_param_name;
el.value=cust_param_value;
document.getElementById("dcapiform").appendChild(el);
}

Kind Regards, Chris

A: 

for whatever purpose ur trying to send ambiguous field names to the server, here is what ur looking for (u should consider a smarter way, may br processing on the server side instead)

var elems = document.getElementsByTagName("input");
var arr = [];
for (var i = 0; i<elems.length; i++){
    if (elems[i].type != "text") continue;
    if (elems[i].name.indexOf("custom_name") < 0) continue;
    var index = parseInt(elems[i].name.substring(11)); // you should rename custom_name to custom_name1, and so for custom_value

    arr[arr.length] = elems[i].value+"=" + elems["custom_value"+index].value;
}
document.forms[0]["passedITems"] = arr.join(",");
docmentt.forms[0].submit();

on your server side, read "passedItems", and split by ",", you get an array of "name=value", split again on "=" you get a sub array of name, and value

Ayyash