Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Subtract calculation

Participant ,
Feb 02, 2009 Feb 02, 2009
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?
TOPICS
Server side applications
724
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 02, 2009 Feb 02, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 03, 2009 Feb 03, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 03, 2009 Feb 03, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 06, 2009 Feb 06, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2009 Feb 06, 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>
>
>
>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 10, 2009 Feb 10, 2009
LATEST
I did take the calc.php off. And tried it again. Same problem.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines