Forum Moderators: open

Message Too Old, No Replies

ie javascript & form errors, returns object instead of value

         

fintan

3:43 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



Hi I've 2 pieces of code. Both of them have to do with gathering all the fields in a form, then making a url out of them.

function UrlString(init){ /* @annotation: Builds the url from any givin form */

// var formElements = document.forms[0]; // Old method
var formElements = document.getElementById(init); // Dynamic method
var urlvar = '';

for(var i = 0;i < formElements.length; i++){
if(formElements[i].value){ /* @annotation: Strips empty values */

urlvar += formElements[i].id + "=" + escape(formElements[i].value) + "&";
}
// else{
// urlvar += formElements[i].id + "=" + formElements[i].value + "&";
// }
}
urlvar = new String(urlvar);
urlvar = urlvar.replace(/undefined/g,"");

return urlvar;
}

The second is pretty much the same but does it manually.

function callurl(){ /* @annotation: Used when updating a call */
//var details = document.getElementById("Details").value;
//alert(document.getElementById("spam").value);
var loggedby = document.getElementById("loggedby").value;
var staff = document.getElementById("Staff").value;
//var priority = document.getElementById("Priority").value;
var category = document.getElementById("Category").value;
var maintenace = document.getElementById("Maintenace").value;
var reference = document.getElementById("Reference").value;
var comments = document.getElementById("Comments").value;
var datelogged = document.getElementById("datelogged").value;
var subcat = document.getElementById("SubCat").value;
var workscarriedout = document.getElementById("workscarriedout").value;
var actionid = document.getElementById("actionid").value;
var historydate = document.getElementById("historydate").value;
var self = document.getElementById("self").value;
//alert(details);
if (document.getElementById("self").checked==true){
self = '(Assigning to self)';
}
var urlvar = /*"workforceid=" + workforceid + */"Details=" + escape(document.getElementById("Details").value) + "&Proirity=" + document.getElementById("Priority").value+ "&Maintenace=" + maintenace + "&Reference=" + reference + "&Comments=" + escape(comments) + "&datelogged=" + datelogged + "&Category=" + category + "&SubCat=" + subcat /*+ "&Guid=" + guid */+ "&Staff=" + staff + "&loggedby=" + loggedby + "&historydate=" + historydate + "&actionid=" + actionid + "&workscarriedout=" + escape(workscarriedout) + "&self=" + self ;
return urlvar;
}

Both of these work fine in all other browsers except ie.
In my form I have an element called Details. When the form is submitted ie classes Details as undefined and an object. Replacing the content with undefined.

I've renamed the element to test and it works fine but causes other elements to become undefined. Can anyone see where I'm going wrong? Thanks

fintan.

Fotiman

4:08 pm on Jan 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't looked too closely at this, but I think this:

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

Should be this:

for(var i = 0;i < formElements.elements.length; i++){

That is, a form has an "elements" collection that contains all of the elements. It looks like you're trying to access the elements directly from the form object.

fintan

7:46 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



Thanks for the reply Fotiman, seems to have done the trick. Thanks