0

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); 
 }
4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by Taywin
0

instead of:

for (item in content)

try:

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

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

0

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.

Edited by Taywin: n/a

0

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.

0

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.

0

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"/>

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.