Builds POST parameters for the form specified - Node.js HTML

Node.js examples for HTML:Form

Description

Builds POST parameters for the form specified

Demo Code


// Builds POST parameters for the form specified
function build_form_data(form){
  this.form = typeof form == 'string' ? document.forms[form] : form;
  
  var parameters = [];
  for(var i = 0; i < this.form.elements.length; i++){
    var element = this.form.elements[i];
    //for now i am adding radio,checkbox(not control item),text and hidden
    //later we can add type = select, type = textarea or checkbox control items
    if((element.type == 'radio' || element.type == 'checkbox') && element.checked){
      parameters.push(element.name + '=' + element.value);          
    }/*www  .  j a v  a2s  . c o m*/
    if(element.type == 'select-one'){
      parameters.push(element.name + '=' + element.options[element.selectedIndex].value);
    }
    if(element.type == 'text' || element.type == 'hidden'){
      parameters.push(element.name + '=' + element.value);          
    }
  }
  parameters = parameters.join('&');
  return parameters;
}

Related Tutorials