This topic is locked

Calculate field in Before Record Added Event

1/17/2008 5:30:25 PM
PHPRunner General questions
L
Lisa2006 author

Table: invoices

Fields: net, vat, gross
Fields are set up as follows in Visual Editor | Add Page | Edit as
vat = read only

gross read only
When user enters a value in the net field, i would like to display the vat value in real time in the vat field.

(vat = 17.50% of net)
Likewise, i would like to display the gross value in real time in the gross field.

(gross = net + vat)
I believe this would be done in the Add Page | Before Record Added event. Just not sure how to apply the code!!!
Thanks in advance
Lisa

J
Jane 1/21/2008

Lisa,
to calculate totals before on the page add some Javascript code to your page.

Add onchange or onblur handlers to your controls for this.

I.e. add this code to your page in HTML mode on the Visual Editor tab:

<script language=javascript>

function calctotal()

{

document.editform.value_vat.value=parseFloat(document.editform.value_net.val

ue)*0.175;

}

value_net.onblur = calctotal;

</script>

L
Lisa2006 author 1/21/2008

Where would i place the code snippet:
&lt;script language=javascript>
function calctotal()

{

document.editform.value_vat.value=parseFloat(document.editform.value_net.value)*

0.175;

}

value_net.onblur = calctotal;

</script>
Currently my /invoices_add.php has shows the following in HTML mode:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>invoices</TITLE>

<META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK

href="include/style.css" type=text/css rel=stylesheet>{$includes}

<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>

<BODY bgColor=white {$bodyonload}>

<FORM name=editform action=invoices_add.php method=post

encType=multipart/form-data {$onsubmit}>{include_if_exists file="include/header.php"}

<H1>invoices, Add new record</H1>{doevent name="AddOnLoad"} {$message}

<DIV align=left>

<HR width=300 noShade SIZE=1>

</DIV>Â Â Â <A href="invoices_list.php?a=return">Back to

list</A><BR><BR>

<TABLE cellPadding=2>

<TBODY>

<TR>

<TD class=shade width=150>net</TD>

<TD width=250>{build_edit_control field="net" mode="add" value=$value_net} </TD></TR>

<TR>

<TD class=shade width=150>vat</TD>

<TD width=250>{build_edit_control field="vat" mode="add" value=$value_vat} </TD></TR>

<TR>

<TD class=shade width=150>gross</TD>

<TD width=250>{build_edit_control field="gross" mode="add" value=$value_gross} </TD></TR>

<TR height=50>

<TD align=middle colSpan=2><INPUT class=button type=submit value=Save name=submit1> <INPUT class=button type=reset value=Reset> <input type=hidden name="a" value="added"></TD></TR></FORM><!-- legend -->

<TR height=50>

<TD align=left colSpan=2>

<HR width=300 noShade SIZE=1>

<BR><IMG src="images/icon_required.gif"> - Required field

</TD></TR></TBODY></TABLE>{include_if_exists file="include/footer.php"} {$linkdata}&lt;script>SetToFirstControl();</script></BODY></HTML>
Thanks Lisa

J
Jane 1/21/2008

Lisa,
sorry for my fault.

You need to add {literal} and {/literal} around your JavaScript code:

{literal}

<script language=javascript>
function calctotal()

{

document.editform.value_vat.value=parseFloat(document.editform.value_net.value)*

0.175;

}

value_net.onblur = calctotal;

</script>

{/literal}

L
Lisa2006 author 1/21/2008

Jane,
I have added the code snippet via the HMTL page as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>invoices</TITLE>

<META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK

href="include/style.css" type=text/css rel=stylesheet>{$includes}

<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>

{literal}

<script language=javascript>

function calctotal()

{

document.editform.value_vat.value=parseFloat(document.editform.value_net.value)*

0.175;

}

value_net.onblur = calctotal;

</script>

{/literal}<BODY bgColor=white {$bodyonload}>

<FORM name=editform action=invoices_add.php method=post

encType=multipart/form-data {$onsubmit}>{include_if_exists file="include/header.php"}

<H1>invoices, Add new record</H1>{doevent name="AddOnLoad"} {$message}

<DIV align=left>

<HR width=300 noShade SIZE=1>

</DIV>Â Â Â <A href="invoices_list.php?a=return">Back to

list</A><BR><BR>

<TABLE cellPadding=2>

<TBODY>

<TR>

<TD class=shade width=150>net</TD>

<TD width=250>{build_edit_control field="net" mode="add" value=$value_net} </TD></TR>

<TR>

<TD class=shade width=150>vat</TD>

<TD width=250>{build_edit_control field="vat" mode="add" value=$value_vat} </TD></TR>

<TR>

<TD class=shade width=150>gross</TD>

<TD width=250>{build_edit_control field="gross" mode="add" value=$value_gross} </TD></TR>

<TR height=50>

<TD align=middle colSpan=2><INPUT class=button type=submit value=Save name=submit1> <INPUT class=button type=reset value=Reset> <input type=hidden name="a" value="added"></TD></TR></FORM><!-- legend -->

<TR height=50>

<TD align=left colSpan=2>

<HR width=300 noShade SIZE=1>

<BR><IMG src="images/icon_required.gif"> - Required field

</TD></TR></TBODY></TABLE>{include_if_exists file="include/footer.php"} {$linkdata}&lt;script>SetToFirstControl();</script></BODY></HTML>
Still cannot see any results!!!
When i enter a value in the net field on the Add Page, it should display the pre-calculated vat value in the vat field. However this is still blank.
PLEASE HELP <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=25035&image=1&table=forumreplies' class='bbc_emoticon' alt=':unsure:' />