Where does this extra name / value pair come from?

advertisements

I have a form:

  <form id="f3" method="post" action="interface_add.php">
    <fieldset>
    <input onclick="this.value=''" class="te3" type="text" name="f3a" value="title"/>
    <input onclick="this.value=''" class="te3" type="text" name="f3b" value="url"/>
    <a id="f3c" class='but' href="javascript:void(0)" onclick="i3()">Add</a>
    <a id="f3d" class='but' href="javascript:void(0)" onclick="i3a()">Delete</a>
    </fieldset>
  </form>

and I use some Javsascript to "serialize" the element names and values like this:

function is(a)
  {
  var b='';
  var c=document.forms[a].elements;
  for(i=0;i<c.length;i++)
    {
    if(c[i].type=='checkbox'&&c[i].checked==false)
      {
      b+=c[i].name+"=NULL&";
      }
    else
      {
      b+=c[i].name+"="+c[i].value+"&";
      }
    }
    b=b.slice(0,-1);
  return b;
  }

which I call from here:

function i3()
  {
  var a='';
  a=is('f3');

However the return value I get from is() inserted into 'a' is

"undefined=undefined&f3a=title&f3b=url"

Funny thing is I had a similar problem previously but this was because I was not intializing 'a' which is why I broke this up, mostly out of paranoia that 'a' was not initialized properly.

Probably something simple I overlooked - but why is there undefined=undefined appearing.


It is coming from the <fieldset> element.

Just add a test for a name property inside the loop.

for(i=0;i<c.length;i++) {

    if( c[i].name ) {

       // your code

    }
}