Retrieving a function value

After I get the total of the 2 subtracted numbers (this works) I want to be able to post that to the next page of a form. I’ve tried sending the $total_due and even subNums but nothing has worked. Any suggetions?
[php]

<?php //this number is coming from the previous page as a URL variable $firstnum= $_POST['price']; //this number comes from the dreamweaver recordset on this page $secondnum=$rs_values->Fields('value'); function subNums ($firstnum, $secondnum){ $total_due=$firstnum - $secondnum; return $total_due; } print subNums($firstnum,$secondnum) ; ?>

[/php]

MOD EDIT: fixed incorrect usage of PHP bbcode tags and formatted for easier reading

from what iv learned you cant send variables out of a function, all you can do is do the math and echo the results. So best bet its to remove the function, altho u could try makign the variable a session (havent done that myself) and that should register on the next page for you. If you dont have sessions on then i suggest a hidden form element.

Thanks! I’ve now tried this:
[php]

<?php //first I declare the two sets of numbers I want to do math on...and tell where their data comes from(price & value) //firstnum and secondnum are names I gave the 2 math variables //note: under $total_due I can change the math to +,-,/(division),*(multiplication) //this number is coming from the previous page as a URL variable $firstnum= $_POST['price']; //this number is coming from the recordset on this page $secondnum=$rs_values->Fields('value'); $total_due=$firstnum - $secondnum; print "Total Due is: $total_due"; ?>

[/php]
and it does still print out the correct subtracted amount. I have added a hidden field:
[php]

<?php

?>
[/php]

and on the next page attempt to get the variable:
[php]

<?php echo $_POST['total_due']; ?>

[/php]

But it’s still not showing up…I’ll keep trying.
Thank!
Lynne
MOD EDIT: fixed incorrect usage of PHP bbcode tags and formatted for easier reading
Consider Viewing
http://phphelp.com/forums/faq.php?sid=53350883bd62d38bf21bb8284ca30289#21
and
http://phphelp.com/forums/viewtopic.php?t=2752&sid=53350883bd62d38bf21bb8284ca30289

How are you trying to send it to the next page? Are you "POST"ing it in a form?

Have you considered using the GET method (in the URL)?

Pablo, that’s not entirely true. Much of it depends on the scope of the variable. http://us3.php.net/variables.scope But passing the info through sessions (or cookies) is another way to do it as well.

It is a form that is posting to the next page. I’ve tried both get and post methods. The get method also did not show up in the url address once posted.

I believe you can pass via post method WITHOUT using a form, however, it’s a bit more complicated

So if you are NOT passing using the html FORM tags, then you need to either investigate the above (quote) or you can pass it via the GET method (in the URL) or as Pablo pointed out you can use sessions.

If these concepts are foreign to you, I suggest that you get a good book on beginning PHP. I found http://www.wrox.com/books/0764543644.shtml to be an excellent beginners book. Although it’s a bit dated (as there is now PHP 5), it will still be very useful.

Having never done anything with sessions up to this point, I finally tried it and with success to my problem:

[php]<?php
session_start(); //first I added this at the top of the page
?>[/php]

Then further down the page where the math needs to occur:

[php]<?php $_SESSION[total_due] = “$result”;
$firstnum=$_POST[‘price’]; //this number is coming from the previous page as a URL variable
$secondnum=$rs_values->Fields(‘value’); //this number is coming from the recordset on this page
$result=$firstnum - $secondnum;
print “$result”;?>[/php]

form then submits to next page to continue. There I once again added the session_start code at top of page. Then I dragged a dreamweaver session variable named total_due to the spot I wanted it to show up:

[php]<?php echo $HTTP_SESSION_VARS['total_due']; ?>[/php]

At the end I used a hidden field that was also bound to the Session variable:

[php]<?php <input name="total_due" type="hidden" id="total_due" value="<?php echo $HTTP_SESSION_VARS['total_due']; ?>">?>[/php][/php]</p> <p>Result: the total_due showed up where I wanted on multiple pages AND ended up in the database when the form was finally submitted!<br> Thanks to all…</p>

Sponsor our Newsletter | Privacy Policy | Terms of Service