0

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

5
  • Easy way is add a class to all the elements and iterate over elements grabbed from the class values. Alternative is grab jQuery and just do $('form#id').serialize() Commented Aug 7, 2010 at 2:11
  • jquery not an option to keep the form in its most basic form. heres the example that isnt working for me: redro.com.au/projects/navTest/cust-params_POST.html Commented Aug 7, 2010 at 3:05
  • The functional requirement is hard to understand. What do you want to do? Commented Aug 7, 2010 at 3:32
  • Use a while loop instead of the two if statements. Commented Aug 7, 2010 at 3:40
  • yea does anyone mind helping me with this? im no developer Commented Aug 7, 2010 at 9:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.