Skip to main content
Inspiring
February 2, 2009
Question

Subtract calculation

  • February 2, 2009
  • 6 replies
  • 724 views
I have a function that calculates the remaining amount from an entered value and the total value in the database:

<?php

function updateTotal()
{
$totalled = ($row_total_cost['Total_Bal'] - $_REQUEST['paid']);
return $totalled;
}
updateTotal();

?>

<form id="form2" name="form2" method="post" action="<?php echo updateTotal(); ?>">
<table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
<tr>
<td width="30" valign="top">Paid:</td>
<td width="152" valign="top"><input type="text" name="paid" id="paid" /></td>
<td width="136" valign="top">New Balance:</td>
<td width="143" valign="top"><?php echo updateTotal();?></td>
</tr>
<tr>
<td valign="top"></td>
<td valign="top"> </td>
<td valign="top"><input type="submit" name="update" id="update" value="Update" /></td>
<td valign="top"> </td>
</tr>
</table>
</form>

What am I doing wrong?
This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

Inspiring
February 10, 2009
I did take the calc.php off. And tried it again. Same problem.
Inspiring
February 6, 2009
The_FedEx_Guy wrote:
> I'm just trying to subtract one number from another.
>
> The one I want to subtract is the one the User enters in the textbox.
>
> I have tried to play around with it. And have followed a Javascript example I
> found online but the value disappers once I press the submit button.
>
> It will show it for a fraction of a second and then blank.
>
> <SCRIPT LANGUAGE="JavaScript">
>
> function CalculateSum(Atext, Btext, form)
> {
> var A = parseFloat(Atext);
> var B = parseFloat(Btext);
> form.Answer.value = B - A;
> }
>
> </SCRIPT>

When you press the submit button you will be directed to calc.php, thus
you need to add a button to your page:
<input type="button" name="calc"
value="Update"
onClick="CalculateSum(this.form.paid.value,
this.form.balance.value, this.form)"/>

Mick.


>
> <form id="form2" name="form2" method="post" action="calc.php">
> <table width="500" border="0" align="center" cellpadding="3"
> cellspacing="3">
> <tr>
> <td width="30" valign="top">Paid:</td>
> <td width="152" valign="top"><input type="text" name="paid"
> id="paid" /></td>
> <td width="136" valign="top">New Balance:</td>
> <td width="143" valign="top"><INPUT TYPE=TEXT NAME="Answer"></td>
> </tr>
> <tr>
> <td valign="top"></td>
> <td valign="top"> </td><input name="balance" type="hidden"
> value="<?php echo $row_total_cost['Total_Bal']; ?>" />
> <td valign="top"><input type="submit" name="update" id="update"
> value="Update" onClick="CalculateSum(this.form.paid.value,
> this.form.balance.value, this.form)"/></td>
> <td valign="top"> </td>
> </tr>
> </table>
> </form>
>
>
>
Inspiring
February 6, 2009
I'm just trying to subtract one number from another.

The one I want to subtract is the one the User enters in the textbox.

I have tried to play around with it. And have followed a Javascript example I found online but the value disappers once I press the submit button.

It will show it for a fraction of a second and then blank.

<SCRIPT LANGUAGE="JavaScript">

function CalculateSum(Atext, Btext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
form.Answer.value = B - A;
}

</SCRIPT>

<form id="form2" name="form2" method="post" action="calc.php">
<table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
<tr>
<td width="30" valign="top">Paid:</td>
<td width="152" valign="top"><input type="text" name="paid" id="paid" /></td>
<td width="136" valign="top">New Balance:</td>
<td width="143" valign="top"><INPUT TYPE=TEXT NAME="Answer"></td>
</tr>
<tr>
<td valign="top"></td>
<td valign="top"> </td><input name="balance" type="hidden" value="<?php echo $row_total_cost['Total_Bal']; ?>" />
<td valign="top"><input type="submit" name="update" id="update" value="Update" onClick="CalculateSum(this.form.paid.value, this.form.balance.value, this.form)"/></td>
<td valign="top"> </td>
</tr>
</table>
</form>

Inspiring
February 3, 2009
On Tue, 3 Feb 2009 09:31:48 +0000 (UTC), "The_FedEx_Guy"
<webforumsuser@macromedia.com> wrote:

>Cant I submit the same page with the calculation?
>
>If I set the action to a different page, it will load that page on submit.

Of course you can submit it back to the same page. I think most of my
forms do that. You need to include some processing logic to determine if
it has been submitted and respond accordingly. I'm just not clear on
exactly what you're trying to accomplish.

Gary
Inspiring
February 3, 2009
Cant I submit the same page with the calculation?

If I set the action to a different page, it will load that page on submit.
Inspiring
February 2, 2009
On Mon, 2 Feb 2009 16:47:53 +0000 (UTC), "The_FedEx_Guy"
<webforumsuser@macromedia.com> wrote:

> What am I doing wrong?

It looks like you're expecting PHP to execute in the browser. PHP
executes on the server, before the server sends a file to the browser.
When the browser gets the file, the PHP is all already done.

The action attribute of the form tag should be set to the file name of a
file that the form's results are submitted to. You cannot just plug some
arbitrary PHP code into it.

Gary