Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why wont this work

Status
Not open for further replies.

recsx

Programmer
Joined
Sep 19, 2004
Messages
11
Location
CA
I am trying to make an invoice form.

Price, Qty, Extended

i have 20 lines, i want to make a function that i can send argument to when the user goes onKeyUp and onKeyDown, i pass this to the function...
onKeyUp="PriceCalc(this.form,this.name,this.form.price1.value,this.form.qty1.value)"

and it always gives me an error, document.forms[...].elements is null or not an object.

Here is my function



function PriceCalc(FromIndex,FromName,Price,Qty){

Extended = (Price * Qty);


var Extended = Math.round(Extended * Math.pow(10,2))/Math.pow(10,2);

if(Extended > '0'){


for (var i=0;i < FromIndex.elements.length;i++)
if (FromIndex.elements.name == FromName){
document.forms.extended1.value = Extended;
}


}else{
document.Invoice.extended1.value = "";
}
}
 
Sounds like you're missing a form with the exact spelling and capitalization that you're passing to the function.

Your code:

if (FromIndex.elements.name == FromName){
document.forms.extended1.value = Extended;
}

indicates you have a number of separate forms on your page, as many as you have form elements in whatever form you're referencing as FromIndex. The error message makes it sound like you don't have that many separate forms.

Try putting

alert(i);

in the first part of the loop to see which form index number the code is erroring on.

Lee
 
Thanks i figured it out.

I also had typos FromIndex was seposed to be FormIndex and FromName was sepose to be FormName.

this is what i now have.

function ExtendedCalc(FormName,Extended,Price,Qty){
CalcExtended = (Price.value * Qty.value);
var CalcExtended = Math.round(CalcExtended * Math.pow(10,2))/Math.pow(10,2);

if(CalcExtended > '0'){
FormName.elements[Extended].value = CalcExtended;
}else{
FormName.elements[Extended].value = '';
}
}

Also, all the fields are in one form, i only have one form.

But i now have another question.

How can a reference a field using a onKeyUp event or any event for that mater without having to point to the specific field?
Simply put if i call a function from within a field with a function can i point to its field without using its name or index number? somehow dynamicaly?

here is what i have.
<TD><INPUT TYPE="text" SIZE="8" NAME="item1" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="60" NAME="itemdesc1" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="price1" onKeyUp="ExtendedCalc(this.form,this.form.extended1.name,this.form.price1,this.form.qty1)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="qty1" onKeyUp="ExtendedCalc(this.form,this.form.extended1.name,this.form.price1,this.form.qty1)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="extended1" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="gst_rate1" onKeyUp="GSTCalc(this.form,this.form.extended1,this.form.gst_rate1,this.form.gst_tot1.name);" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="pst_rate1" onKeyUp="PSTCalc(this.form,this.form.extended1,this.form.pst_rate1,this.form.pst_tot1.name);" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="gst_tot1" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="pst_tot1" READONLY CLASS="MyFields"></TD>

This is all in one FORM and i have about 20 lines of this each being named differently such as price1,price2 so on and so forth.
But me being hard headed i always try and make things dynamic so that in the future if i want to add stuff i dont have to do much work and i would rather not have to point to each field but rather dynamicaly.

I am not done with this yet, for now i just want to get things working and then try and make all fields as an array such as NAME="price[]" and maybe re-write my function to calculate the whole array.

I want to make it this way so that maybe later i may want to add more lines or remove them simply by just copying or deleting line and not having to modify any of the scripts.

Am i going to far with this???

Thanks a bunch.
 
If this is the same question you're asking in the thread thread216-1180140, then please stick to one thread only. Cross-posting, or duplicate posting only serves to annoy, frustrate, and get you less help, not more.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ya sorry, just i figured i was not describing it properly the first time and figured i would clear it up a bit.

Thanks i think i got all i need for now.

I got a reply in the other thread about stripping characters, if i need more help i will post again, but for now i can consider this thread closed.

Thanks
 
I've done it.

I've accomplished what i was trying to do thanks to the help of this great forum.

Thanks guys.

Here is my entire HTML file along with the script.
I did not add all 20 lines in the invoice yet but you get the picture, it now works as it should and with the ability to add and remove as many lines(rows) as you like without having to point to specific fields.



CODE::

<HTML>

<HEAD>
<META HTTP-EQUIV="Content-Language" CONTENT="en-us">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="css/default.css">
<TITLE>Logo</TITLE>
</HEAD>

<BODY>
<?
$HeaderBGcolor1 = "9DAFFF";
?>

<SCRIPT Language="JavaScript">
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function RowItemCalc(FormName,RowNum){
RowNum = RowNum.toString().replace(/[^0-9]*/,'');

price = 'price'+RowNum;
qty = 'qty'+RowNum;
gst_rate = 'gst_rate'+RowNum;
gst_tot = 'gst_tot'+RowNum;
pst_rate = 'pst_rate'+RowNum;
pst_tot = 'pst_tot'+RowNum;
extended = 'extended'+RowNum;

CalcExtended = (FormName.elements[price].value * FormName.elements[qty].value);
var CalcExtended = Math.round(CalcExtended * Math.pow(10,2))/Math.pow(10,2);

if(CalcExtended > '0'){
FormName.elements[extended].value = CalcExtended;
}else{
FormName.elements[extended].value = '';
}

CalcGSTRate = (FormName.elements[gst_rate].value * FormName.elements[extended].value);
var CalcGSTRate = Math.round(CalcGSTRate * Math.pow(10,2))/Math.pow(10,2);
if(CalcGSTRate > '0'){
FormName.elements[gst_tot].value = CalcGSTRate;
}else{
FormName.elements[gst_tot].value = '';
}

CalcPSTRate = (FormName.elements[pst_rate].value * FormName.elements[extended].value);
var CalcPSTRate = Math.round(CalcPSTRate * Math.pow(10,2))/Math.pow(10,2);
if(CalcPSTRate > '0'){
FormName.elements[pst_tot].value = CalcPSTRate;
}else{
FormName.elements[pst_tot].value = '';
}
}
</SCRIPT>


<FORM NAME="Invoice" METHOD="POST" ACTION="">
<TABLE BORDER="0" WIDTH="750" CELLPADDING="0" CELLSPACING="0" ALIGN="center">
<TR>
<TD WIDTH="375">Logo</TD>
<TD>
<TABLE BORDER="1" WIDTH="375" CELLPADDING="0" CELLSPACING="0" CLASS="TableStyle1">
<TR>
<TD WIDTH="150" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Invoice #:</TD>
<TD>&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Date:</TD>
<TD>&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Per Quote #:</TD>
<TD>&nbsp;</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>

<BR>

<TABLE BORDER="0" WIDTH="750" CELLPADDING="0" CELLSPACING="0" ALIGN="center">
<TR>

<TD ALIGN="left">
<TABLE BORDER="1" WIDTH="374" CELLPADDING="0" CELLSPACING="0" CLASS="TableStyle1">
<TR>
<TD WIDTH="374" COLSPAN="4" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Sold To:</TD>
</TR>
<TR>
<TD WIDTH="80" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;First Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Last Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Company Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Address:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;City/Town:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD WIDTH="80" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Province/State:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Country:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Postal/Zip Code:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Phone:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Fax:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
</TABLE>
</TD>

<TD ALIGN="right">
<TABLE BORDER="1" WIDTH="374" CELLPADDING="0" CELLSPACING="0" CLASS="TableStyle1">
<TR>
<TD WIDTH="374" COLSPAN="4" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Ship To:</TD>
</TR>
<TR>
<TD WIDTH="80" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;First Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Last Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Company Name:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Address:</TD>
<TD COLSPAN="3"><INPUT TYPE="text" SIZE="50" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;City/Town:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD WIDTH="80" BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Province/State:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Country:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Postal/Zip Code:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Phone:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Fax:</TD>
<TD><INPUT TYPE="text" SIZE="17" NAME="" CLASS="MyFields"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>

<BR>

<TABLE BORDER="1" WIDTH="750" CELLPADDING="0" CELLSPACING="0" ALIGN="center" CLASS="TableStyle1">
<TR>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Ship Via</TD>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">FOB</TD>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">P.O. #</TD>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Terms</TD>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Salesperson</TD>
<TD ALIGN="center" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Page</TD>
</TR>
<TR>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
</TR>
</TABLE>

<BR>

<TABLE BORDER="1" WIDTH="750" CELLPADDING="0" CELLSPACING="0" ALIGN="center" CLASS="TableStyle1">
<TR>
<TD ALIGN="center" WIDTH="50" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Item</TD>
<TD ALIGN="center" WIDTH="310" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Item Description</TD>
<TD ALIGN="center" WIDTH="60" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Price</TD>
<TD ALIGN="center" WIDTH="50" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Qty.</TD>
<TD ALIGN="center" WIDTH="60" BGCOLOR="#<?echo $HeaderBGcolor1;?>">Extended</TD>
<TD ALIGN="center" WIDTH="50" BGCOLOR="#<?echo $HeaderBGcolor1;?>">GST</TD>
<TD ALIGN="center" WIDTH="50" BGCOLOR="#<?echo $HeaderBGcolor1;?>">PST</TD>
<TD ALIGN="center" WIDTH="60" BGCOLOR="#<?echo $HeaderBGcolor1;?>">GST Tot</TD>
<TD ALIGN="center" WIDTH="60" BGCOLOR="#<?echo $HeaderBGcolor1;?>">PST Tot</TD>
</TR>
<TR>
<TD><INPUT TYPE="text" SIZE="8" NAME="item1" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="60" NAME="itemdesc1" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="price1" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="qty1" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="extended1" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="gst_rate1" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="pst_rate1" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="gst_tot1" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="pst_tot1" READONLY CLASS="MyFields"></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" SIZE="8" NAME="item2" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="60" NAME="itemdesc2" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="price2" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="qty2" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="extended2" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="gst_rate2" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="8" NAME="pst_rate2" onKeyUp="RowItemCalc(this.form, this.name)" CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="gst_tot2" READONLY CLASS="MyFields"></TD>
<TD><INPUT TYPE="text" SIZE="10" NAME="pst_tot2" READONLY CLASS="MyFields"></TD>
</TR>
</TABLE>

<BR>

<TABLE BORDER="0" WIDTH="750" CELLPADDING="0" CELLSPACING="0" ALIGN="center">
<TR>
<TD>Notes:<BR>
<TEXTAREA ROWS="2" NAME="S1" COLS="20"></TEXTAREA>
</TD>
<TD>Thank you for choosing<BR>
This Company<BR>
please come again.</TD>
<TD WIDTH="150">

<TABLE BORDER="1" WIDTH="150" CELLPADDING="0" CELLSPACING="0" ALIGN="center" CLASS="TableStyle1">
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Subtotal:</TD>

<TD WIDTH="50"><INPUT TYPE="text" SIZE="10" NAME="subtotal" READONLY CLASS="MyFields"></TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;GST:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;PST:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Shipping:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Total:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Deposit:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
<TR>
<TD BGCOLOR="#<?echo $HeaderBGcolor1;?>">&nbsp;Balance:</TD>
<TD WIDTH="50">&nbsp;</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>

</FORM>

</BODY>

</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top