Click to See Complete Forum and Search --> : Javscript to clear PHP form fields


justrick
10-31-2003, 12:49 PM
I have a PHP calculating form that shows zeros by default in the text input fields. I wrote a simple Javascript to remove the zeros from the fields on load so the user doesn't have to highlight them before typing the numbers in. The script works fine in IE4+, but will not work in Netscape 4.76, 6.0 or 7.0.
This is the script:

<script type="text/javascript">
function ClearFields(){
if (myform.txtFldA.value=="0"){
myform.txtFldA.value="";
}
if (myform.txtFldB.value=="0"){
myform.txtFldB.value="";
}
if (myform.txtFldC.value=="0"){
myform.txtFldC.value="";
}
if (myform.txtFldD.value=="0"){
myform.txtFldD.value="";
}
return true
}
</script>

<body onLoad="ClearFields()">

Is this a Netscape thing, or am I doing someting wrong?
(BTW, I'm a newbie)

SnowCrash
11-01-2003, 04:37 AM
It might help if you posted also your form. Is the form itself in a div tag or something ? Try also using "document.myform.textFldA.value" instead of "myform.txtFldA.value" only.

justrick
11-01-2003, 05:58 AM
The form is in div tags and a table also. Here is the form (attached)

jrbp
11-01-2003, 06:00 AM
you didn't attach it

justrick
11-01-2003, 06:05 AM
I THOUGHT I did.. I'll try again.

SnowCrash
11-01-2003, 06:41 AM
for IE4+, NS6.1+ and Mozilla this works

document.forms.estCalc.txtFldA.value

but for Ns4+ you would have to do something like:

document.forms.document.NameDiv1.document.NameDiv2.estCalc.txtFldA.value

You would have to name all div elements and then access the form element as shown above by referencen all nested divs --> avoid div's to use for simple center, use table align instead for NS4

justrick
11-01-2003, 10:26 AM
If I rebuild the form without div tags, would document.forms.estCalc.txtFldA.value
work in NS4? And, if I use nested tables, will it cause the same problem?

SnowCrash
11-01-2003, 10:44 AM
tables wouldn't make nesting necessary as do div tags

justrick
11-01-2003, 11:08 PM
Yes! I rebuilt the form using tables only, document.estCalc.txtFldA.value,
and changed onLoad to onLoad="ClearFields(document.estCalc);. It works flawlessly in NS4+ and IE4+.
I attached a copy of the working form. Thanks for your help!

Rick

SnowCrash
11-02-2003, 04:06 AM
:) :) :)