Greetings,
I am trying to solve a problem that has been inflicting my self
created Order Forms for a long time, where the problem is that as I
cannot reproduce this error myself, then it is difficult to know what
is going on.
One of these Order Forms you can see here...
http://www.cardman.co.uk/orderform.php3
These forms are created using HTML, JavaScript and PHP3, where it is
the JavaScript section where this problem is coming from.
What exactly is going on is that most browsers can calculate the Total
Item Weight (see my Order Form boxes) perfectly correctly, but one or
more browsers is producing the wrong answer, where they usually return
zero grams (0g) as the Total Item Weight by mistake.
The calculation steps that it goes through are quite easy as I will
now explain.
First of all the individual item weights are stored in the array
ItemW, where element zero is left as zero, when the real item weights
start from element one.
Not to forget that if a customer selects an item and then changes
their mind and deselects it, then the line change is called using this
position zero as the reference. No values here and so there is nothing
to display and the old values are blanked out.
These item weights are only used when the Order Form has to add up all
the lines and produce your end Total Item Weight.
The JavaScript code that adds up each order form line is this one...
WArray [0] = ItemW [document.Details.Code1.selectedIndex] *
document.Details.Qty1.value;
WArray is just my temporary array in order to store the combined item
weight for each line. This was defined at the start of the JavaScript
section as...
var WArray=new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
So that line of course reads the individual item weights from my ItemW
array, where it multiplies it by the Quantity that the customer
requires, which they can directly enter on my Order Form.
Sure the user could enter junk in this field instead of the required
number, but that I highly doubt is the problem here.
If you see my Order Forms, then parseInt is also included within this
line, but that was just my old attempt at solving this problem. As
that attempt failed, then that function is not needed, which is why I
removed it here.
And so we now come down to this section...
CurrWeight = WArray [0] + WArray [1] + WArray [2] + WArray [3] +
WArray [4] + WArray [5] + WArray [6] + WArray [7] + WArray [8] +
WArray [9] + WArray [10] + WArray [11] + WArray [12] + WArray [13] +
WArray [14];
document.Details.IWeight.value = CurrWeight + "g";
What happens here is that all the line weights now stored in WArray
are added together producing our required Total Item Weight, which is
first stored in CurrWeight before being inserted into the Order Form
in the Total Item Weight box.
WArray needs to be used and defined within the start global section
upon page load, when one line is modified at a time before all the
whole lot are added up upon each call.
Simple enough code you may think, but some browsers without doubt are
messing this up. The most common result is that items have been
selected on the Order Form, but the Total Item Weight has been
incorrectly calculated as zero grams.
One other problem that I noticed recently may go to help explain this
problem, when the Total Item Weight was lower than what it should have
been. After manually calculating the values, then all the item weight
lines had been included in the Total Item Weight calculation except
for the first line on the Order Form.
Combined with previous things that I have seen before, then I suspect
what may be happening is as follows...
A ten element JavaScript array should be created as...
0,1,2,3,4,5,6,7,8,9
However, the faulty browsers may be creating their ten element array
like this instead...
1,2,3,4,5,6,7,8,9,10
That if true could explain why my Total Item Weight is being
calculated incorrectly, when writes to and reading from element zero
would result in a lost value.
My first question would be if there are any known faulty browsers who
create arrays like this?
Should this not be the problem, then I am again stumped, when I would
not have a clue why this simple code is failing on some browsers.
The only other aspect I can think of is if this page is exited and
then returned to, when who can say if all the values are restored?
If it is true, then that also poses the problem of what to do to fix
it, when it is a little tricky to know the structure of the array
before you come to use it.
I think that I may be able to create larger arrays if needed and start
from position one instead of zero, but I would have to see if this can
work out.
Anyway, if anyone has a clue what is going on, then such a mention
would be much appreciated, when I am now getting very annoyed at how
the Postal Charge sometimes gets under valued.
Thanks,
Cardman
http://www.cardman.com http://www.cardman.co.uk