/    Sign up×
Community /Pin to ProfileBookmark

Why are the value doesn’t calculate when button press

hye..

i’m trying to calculate value of balance by substract cash to total. But nothing happen.

here is my code:

[code=html]
<script type=”text/javascript”>

function substract(){

{
document.calculate.Cash.value = ZA;
document.calculate.Total.value = Z1;
balance();
}

function balance(){
AAA = document.calculate.Balance;
AAA.value = (ZA-Z1).toFixed(2);
}

</script>

</head>
<body style=”background-color: #99FF99″>
<?php
// if there are any errors, display them
if ($error != ‘Unable to update’)
{
echo ‘<div style=”padding:12px; border:1px solid purple; color:red; font-weight: bold; text-align: center; font-size: medium; “>’.$error.'</div>’;
}
?>

<form action=”” method=”post” name=”calculate” style=”width: 1320px; “>
<div class=”style1″>
<input type=”hidden” name=”CustID” value=”<?php echo $CustID; ?>”>
<input type=”hidden” name=”Total” value=”<?php echo $Total; ?>”>
<input type=”hidden” name=”TableID” value=”<?php echo $TableID; ?>”>
<div class=”style5″ style=”width: 820px; height: 148px; margin-left: auto; margin-right: auto;”>
<p class=”style9″>&nbsp;</p>
<p class=”style17″><span class=”style19″>CUSTOMER </span> <strong>
<span class=”style19″>ID:</span></strong> <strong> <?php echo $CustID; ?>
</strong></p>
<table style=”width: 35%; height: 185px; margin-left: auto; margin-right: auto;” class=”style15″>
<tr>
<td class=”style11″ style=”width: 111px; height: 30px;”><strong>Table ID :
</strong></td>
<td style=”height: 30px; width: 169px;” class=”style18″><strong><?php echo $TableID; ?></strong></td>
</tr>
<tr>
<td class=”style1″ style=”width: 111px; height: 20px;”></td>
<td style=”height: 20px; width: 169px;” class=”style1″></td>
</tr>
<tr>
<td class=”style16″ style=”width: 111px; height: 30px;”><strong>Total :
</strong></td>
<td style=”height: 30px; width: 169px;” class=”style18″><strong>
RM<?php echo $Total; ?> </strong></td>
</tr>
<tr>
<td class=”style1″ style=”width: 111px; height: 20px;”></td>
<td style=”height: 20px; width: 169px;” class=”style1″></td>
</tr>
<tr>
<td class=”style16″ style=”width: 111px; height: 30px;”><strong>Cash :
</strong></td>
<td style=”height: 30px; width: 169px;” class=”style1″> <strong>
<span class=”style20″>RM</span>
<input type=”text” name=”Cash” id=”Cash” class=”style17″ style=”width: 120px”></strong></td>
</tr>
<tr>
<td class=”style1″ style=”width: 111px; height: 20px;”></td>
<td style=”height: 20px; width: 169px;” class=”style1″></td>
</tr>
<tr>
<td class=”style16″ style=”width: 111px; “><strong>Balance :
</strong></td>
<td style=”width: 169px;” class=”style1″><strong>
<span class=”style20″>RM</span>
<input type=”text” name=”Balance” id=”Balance” value=”0.00″ class=”style17″ style=”width: 120px”></strong></td>
</tr>
</table>
<p class=”style5″><strong>
<input class=”style3″ name=”calculate” type=”button” onclick=”substract()” value=”Calculate”>&nbsp;&nbsp;&nbsp; <input class=”style3″ name=”submit” type=”submit” value=”Done”></strong> </div>
</div>
</form>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JohnnyCKMay 16.2011 — I am a JavaScript noob but Ill give it a guess anyway. variable ZA and Z1 are limited to the scope of subtract() so balance cannot use them.

Try making them global variables by moving them outside the subtract function. Comment out the subtract() and put this above it.

var ZA, Z1;

document.calculate.Cash.value = ZA;

document.calculate.Total.value = Z1;

Again I just started teaching myself JavaScript a few days ago so theres a good chance this wont work.
Copy linkTweet thisAlerts:
@KorMay 16.2011 — I am a JavaScript noob but Ill give it a guess anyway. variable ZA and Z1 are limited to the scope of subtract() so balance cannot use them.

Try making them global variables by moving them outside the subtract function. Comment out the subtract() and put this above it.

var ZA, Z1;

document.calculate.Cash.value = ZA;

document.calculate.Total.value = Z1;

Again I just started teaching myself JavaScript a few days ago so theres a good chance this wont work.[/QUOTE]

Even so: ZA and Z1 are undefined. [B]stepbystep[/B], where are those variables ZA and Z1 coming from?
×

Success!

Help @stepbystep spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...