PDA

View Full Version : document.object problem in form


omgwtf
09-09-2004, 10:28 PM
I have a weird problem that maybe someone here can help me with. I'm writing a large PHP web based application which includes some javascript for added functionality. My problem is that when a certain form (comprised of radio buttons and checkboxes) used to edit or delete items from the database is submitted using the Edit button :

<input type=\"radio\" name=\"edit\" value=\"$eventnum\">
<input type=hidden name=\"numres\" value=\"\">
<input type=hidden name=\"eventid\" value=\"\">
<input name=\"eventaction\" value=\"Edit\" type=\"submit\" onClick=\"return editev($num);\">

the editev() javascript function called by onClick generally works, but only if the form contains 2 or more objects. If the function is called with only one object in the list, e.g. onClick editev(1); then document.eventm.edit[i] returns undefined instead of the object referred to. But.. when 2 or more objects are submitted to the javascript function, all objects are identified correctly with no "undefined"s

here is the javascript function:

function editev(rnum){
var n = 0;
for(i = 0; i < rnum; i++){

//following 2 lines are for debugging problem
rval = document.eventm.edit[i];
alert(\"i is = \" + i + \" and \" + rval);

if (isChecked(document.eventm.edit[i])){
n++;

}
}
if (n == 0) {
alert(\"No events have been selected!\");
return false;
} else if (n == 1) {
document.eventm.numres.value = n;
return true;
}
}

Please help!

sad69
09-10-2004, 12:09 AM
Is this online someplace for us to take a look?

Ideally I'd like to be able to generate this page with more than 2 radios, 2 radios, and 1 radio and look at the Javascript code generated and view the errors.

In this way you can see why it works for the different cases and if not, add some additional debugging code.

Sadiq.

Willy Duitt
09-10-2004, 01:03 AM
Your for loop is assuming that rnum is an array...
An array consists of two or more items... If there is only one item it is no longer an array...

You will need to add a conditional to check if the length of rnum is one or more than one and act accordingly....

.....Willy

omgwtf
09-10-2004, 02:56 AM
Your for loop is assuming that rnum is an array...
An array consists of two or more items... If there is only one item it is no longer an array...

You will need to add a conditional to check if the length of rnum is one or more than one and act accordingly....

.....Willy

Thanks for your help.
My problem was solved by changing the contents of the for loop to the following:

if (!document.eventm.edit.length){
if (isChecked(document.eventm.edit)){
n++;
}

} else {
if (isChecked(document.eventm.edit[i])){
n++;
}
}
now all is well