Javascript code that works in Firefox but not in IE

Discussion in 'JavaScript' started by knkk, Jul 25, 2007.

  1. #1
    I have a link called "Save" on a page of mine, which, when clicked on (the url is typically like this)
    javascript:savesubmit('form31','results31','1','movies','178')
    Code (markup):
    , calls a javascript function called "savesubmit", which in turn calls a script using AJAX (the script URL is http://www.mysite.com/section/myspace/save.php)

    This is the JavaScript function savesubmit:

    function savesubmit(formname, loc, noofvenues, which, mapid)
    {
    	var f = document.forms[formname];
                 alert(f);//this alert was to test if the form name was coming in properly
    	var params = "";
    	for (i=0;i < (f.elements.length);i++) {
    		params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&'; 
    	}
    
    	shows = loc;
    	xmlHttp=GetXmlHttpObject();
    	
    	if (xmlHttp==null)
    	{
    		alert ("Browser does not support HTTP Request");
    		return;
    	}
    	var url = 'http://www.mysite.com/section/myspace/save.php/'+which+'/'+f.eventid.value+'/1/'+mapid+'/'+noofvenues
    	var eventid = f.eventid.value;
    	url = url + "?" + params;
    	var update_loc = eventid + 'save';
    	var update_form = eventid;
    	xmlHttp.onreadystatechange=stateChangedALL;
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    	updatesave(update_form, update_loc, noofvenues, which, mapid);
    }
    Code (markup):
    The form is in a div tag with id 'form31', and the result will be sent to a div tag with id 'results31'.

    The problem is, this thing is working in IE but not in FF. When I click on the Save link, it says "'elements.length' is null or not an object". And the alert I put for testing says [object] instead of giving the form name (both in IE and in FF).

    Here is the <td> which has the forms etc.

    
    <TD WIDTH="510" BGCOLOR="#FFFFFF" VALIGN="TOP" STYLE='padding-right: 10'>
    	<A HREF="/profile/movies/31"><SPAN CLASS="Basic_LARGE_BLUE_LINK">Cheeni Kum</SPAN></A> 
    	<BR>
                 <SPAN CLASS="Basic_LARGE_NORMAL">64-year-old Buddhadeb is single and lives with mom. Now, aren't the possibilities endless?</SPAN>
    	<BR>
                 <BR><A HREF='/profile/locations/178'>PVR Cinema (Punjagutta)</A> (13.3 km)
    	<BR><BR>
    	<DIV id="31save" STYLE="float:left">
    		<A HREF="/profile/movies/31/1">Rate</A> • 
    		<A HREF="javascript:savesubmit('form31','results31','1','movies','178')">Save</A> • 
    		<A HREF="/profile/locations/178/4">Map</A>							<DIV STYLE="clear:both;"></DIV>
    	</DIV>
    
    	<FORM NAME="deleteform31" STYLE="margin:0px">
    		<INPUT TYPE="hidden" NAME="eventid" VALUE="31">
    		<INPUT TYPE="hidden" NAME="edittype" VALUE="delete">
    	</FORM>
    	<FORM NAME="form31" STYLE="margin:0px">
    		<INPUT TYPE="hidden" NAME="eventid" VALUE="31">
    		<INPUT TYPE="hidden" NAME="edittype" VALUE="update">
    	</FORM>
    	<DIV STYLE="clear:both"></DIV>
    	<DIV id="results31" ></DIV>
    </TD>
    
    Code (markup):
    Can someone kindly help? Thank you for your time!
     
    knkk, Jul 25, 2007 IP
  2. Drag Racer

    Drag Racer Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    does you form have the correct name??

    this snip... document.forms[formname]

    needs to match your form

    such as <form name='thisForm'> would change the above code snip to document.forms[thisForm]
     
    Drag Racer, Jul 25, 2007 IP
  3. knkk

    knkk Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey Drag Racer,

    Thank you very much for the reply and your time. Actually, I figured out the error. The <TD> that I gave there is one of a series of rows, each of which has this link for "Save", calling the JavaScript. So there are several rows, and the first variable of the savesubmit function is a form name, with "form" suffixed by a number which is the ID of the record displayed in that row (e. g. "form31"). This form appears in that <TD> itself, so each <TD> has a form named this way.

    There was another row in the same page with the same form name (like "form31"), since that record appeared twice in that page for whatever reason. So when I clicked on that "Save" link, the JS function did not know which form to access. IE just kept saying what I said it did, while FF did not give up - it performed the action on the first form with that name that it encountered.

    I wasted your time with a screw-up from my side, I guess . Thank you again for your time. Cheers!
     
    knkk, Jul 26, 2007 IP
  4. Drag Racer

    Drag Racer Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no problem, I gave a bad reply anyway... didn't notice the variable 'formname' in the function call
     
    Drag Racer, Jul 26, 2007 IP