Hi there, I'm making a script that gets a form's elements, and writes them to an XMLHttpRequest. It works perfectly with inputs, but when it switches to text areas, it returns "undefined" :\
Could you help me find the error? :D

function sendForm(target, form) 
 { 
 var item; 
 var string=""; 
 var content=form.elements; 
 
 for (item in content) 
  { 
  if (content[item].tagName=="INPUT") 
   { 
   if (content[item].type!="submit" && content[item].type!="radio") 
    { 
    if (item!=content.length-1) 
     { 
     string=string+content[item].name+"="+content[item].value+"&"; 
     } 
    if (item==content.length-1) 
     { 
     string=string+content[item].name+"="+content[item].value; 
     } 
    } 
   if (content[item].type=="radio" && content[item].checked==true) 
    { 
    if (item!=content.length-1) 
     { 
     string=string+content[item].name+"="+content[item].value+"&"; 
     } 
    if (item==content.length-1) 
     { 
     string=string+content[item].name+"="+content[item].value; 
     } 
    } 
   } 

  if (content[item].tagName=="TEXTAREA") 
   { 
   if (item!=content.length-1) 
    { 
    string+=content[item].name+"="+content[item].value+"&"; 
    } 
   if (item==content.length-1) 
    { 
    string+=content[item].name+"="+content[item].value; 
    } 
   } 
  } 

 if (string[string.length-1]=="&") 
  { 
  string=string.substr(0, string.length-1); 
  } 

 if (window.XMLHttpRequest) 
  { 
  request=new XMLHttpRequest(); 
  } 
 else 
  { 
  request=new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
 request.open("POST", target, true); 
 request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
 request.send(string); 
 }

instead of:

for (item in content)

try:

for (var item=0,limit=content.length; item < limit; ++item)

instead of:

for (item in content)

try:

for (var item=0,limit=content.length; item < limit; ++item)

It still doesn't include the textarea's value =(
There's something weird though, it includes the NAME of the textarea, but not the value! :O

I don't know... The way you access DOM is not intuitive. I am not sure that you would be getting what you want using for (item in content). Do you really know what item is? I used Firebug, it gave me all including function pointers. If you want only 'input' or 'textarea' tag, use getElementsByTagName() instead. It would be safer in my opinion...

Also, I don't know how you implement your HTML part, so I don't know what you would get from using in your form content.

I don't know... The way you access DOM is not intuitive. I am not sure that you would be getting what you want using for (item in content). Do you really know what item is? I used Firebug, it gave me all including function pointers. If you want only 'input' or 'textarea' tag, use getElementsByTagName() instead. It would be safer in my opinion...

Also, I don't know how you implement your HTML part, so I don't know what you would get from using in your form content.

Item is the number of the item that it is currently on in the loop.

It gets the textarea's name, but not the value...

I tried your code out and am able to return a string which has the name and the value of the textarea field, could you provide your html, maybe something is wrong in their.

Your textarea should look SIMILAR to the following: <textarea name="comments" rows="7" cols="60">Hello</textarea> These are WRONG: <textarea name="comments" rows="7" cols="60" value="HELLO"></textarea> <textarea name="comments" rows="7" cols="60" value="HELLO"/>

Not to offend you, the code below uses getElementsByTagName() which should be more intuitive to others. Also the way to built param string would not require string manipulation at the end.

function sendForm(target, form) { 
//  var item;
//  var string="";
  var content=form.elements;
  var inputElems = form.getElementsByTagName("input");
  var textareaElems = form.getElementsByTagName("textarea");
  var params = new Array();

  for (iEl in inputElems) {
    if (iEl.value.replace(/\s+/g,"")!="") {
      if (iEl.getAttribute("type").toLowerCase()=="text") {
        params.push(iEl.getAttribute("name")+"="+iEl.value)
      }
      else if ((iEl.getAttribute("type").toLowerCase()=="radio") && iEl.checked) {
        params.push(iEl.getAttribute("name")+"="+iEl.value)
      }
    } // not an empty input
  }

  for (tEl in textareaElems) {
    if (tEl.value.replace(/\s+/g,"")!="") {
      params.push(tEl.getAttribute("name")+"="+tEl.value)
    } // not an empty input
  }

  if (window.XMLHttpRequest) { 
    request=new XMLHttpRequest(); 
  } 
  else { 
    request=new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
  request.open("POST", target, true); 
  request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
  request.send(params.join("&"));


/*
  // original code
  for (item in content) {
    if (content[item].tagName=="INPUT") {
      if (content[item].type!="submit" && content[item].type!="radio") {
        if (item!=content.length-1) {
          string=string+content[item].name+"="+content[item].value+"&";
        }
      if (item==content.length-1) {
        string=string+content[item].name+"="+content[item].value;
      }
    }
    if (content[item].type=="radio" && content[item].checked==true) {
      if (item!=content.length-1) {
        string=string+content[item].name+"="+content[item].value+"&";
      }
      if (item==content.length-1) {
        string=string+content[item].name+"="+content[item].value;
      }
    }

    if (content[item].tagName=="TEXTAREA") {
      if (item!=content.length-1) {
        string+=content[item].name+"="+content[item].value+"&";
      }
      if (item==content.length-1) {
        string+=content[item].name+"="+content[item].value;
      }
    }
  }

  if (string[string.length-1]=="&") {
    string=string.substr(0, string.length-1);
  }

  if (window.XMLHttpRequest) {
    request=new XMLHttpRequest();
  }
  else {
    request=new ActiveXObject("Microsoft.XMLHTTP");
  }
  request.open("POST", target, true);
  request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  request.send(string);
*/
}

hielo comment may be what you are looking for as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.