Jump to content

[SOLVED] PHP Help needed


Vibralux

Recommended Posts

Right Guys,

 

I have this code and for some reason its not working. Can any of you spot any mistakes or somthing missing?

 

 

<h3 align="center" class="style1"> Instructions </h3>
<div align="center"><span class="style1">
<div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br>
  The Date of Renewal must be as such - 01 01 2000
    <table width="400px" style="border" bgcolor="#808080">
    </span>
  </div>
</div>
<tr>
<td>
<P align="center">
<form method='post'>
<div align="center">
<div align="center"><span class="style1">Cost per month </span>
  <input type=text name=cpm>
    <br>
  <span class="style1">Date of Renewal:  Day</span>
  <input type=text maxlength=2 size=2 name=rday> 
    <span class="style1">Month</span>
  <input type=text maxlength=2 size=2 name=rmonth> 
    <span class="style1">Year</span>
  <input type=text maxlength=4 size=2 name=ryear>
  <br>
  
  <input type=submit name='calculate' value='Calculate'>
  <input type=reset name='reset' value='Reset'>
  
  <?php
	if (isset($_POST["calculate"]) && $cpm='')
echo "Please enter a number into the Cost per month box";

$todaysdate = time();

if (isset($_POST["calculate"]) && $rday=''){
echo "Please enter a numberinto the Renewal Day Date.";}

if (isset($_POST["calculate"]) && $rmonth=''){
echo "Please enter a numberinto the Renewal Month Date.";}

if (isset($_POST["calculate"]) && $ryear=''){
echo "Please enter a numberinto the Renewal Year Date.";}

$costpermonth = $_GET[cpm];
$renewalday = $_GET[rday];
$renewalmonth = $_GET[rmonth];
$renewalyear = $_GET[ryear];

$renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0);





$timedifference = $renewalseconds - $todaysdate;


$VAT = (int)($costpermonth/100)*17.5;

$price = $costpermonth / $timedifference;

$totalprice = $price + $VAT;

//Output final calculations

echo"<br>";
echo"<br>";
echo"<br>";
print_r('$VAT, $price, $totalprice');

if (isset($_POST["calculate"])) {echo "The total price is $totalprice";}
echo"<br>";
if (isset($_POST["calculate"])) {echo "The price without VAT is $price";}
echo"<br>";
if (isset($_POST["calculate"])) {echo "The VAT is $VAT";}
echo"<br>";

?>
</div>

 

Im not sure if ive used time and mktime wrong. Please help!

Link to comment
https://forums.phpfreaks.com/topic/59411-solved-php-help-needed/
Share on other sites

You set your variables using $_GET['x']; here:

$costpermonth = $_GET[cpm];
$renewalday = $_GET[rday];
$renewalmonth = $_GET[rmonth];
$renewalyear = $_GET[ryear];

 

When your form for submitting uses POST, try changing them to:

 

$costpermonth = $_POST[cpm];
$renewalday = $_POST[rday];
$renewalmonth = $_POST[rmonth];
$renewalyear = $_POST[ryear];

 

Also to tidy your code a bit, at the end use:

if (isset($_POST["calculate"])) {
echo "The total price is $totalprice<BR>";
echo "The price without VAT is $price<BR>";
echo "The VAT is $VAT<BR>";
}

 

Any better now?

The code is supposed to be taking what is put into the form out of the form and then play with it.

 

I want to find out how much its gonna cost to upgrade a server or somthing in the middle of a persons contract. The script should automatically get todays date and then get the date that was inputted into the form.

 

Then it should take those away from each other to give somthing that i can use later.

 

Then it does some more calculations.

 

Then it should ouput them in an echo.

OK, the calculations work now. But it comes up with the print_r bit which i dont want. It says i have to have somthing in there so what do i have in there?

 

Also, now the calculations work when i click calculate i get the numbers but i get an E-06 at the end of the number. Does any one know what this is/means?

Not sure what the (int) is here for:

$VAT = (int)($costpermonth/100)*17.5;

 

echo 'Or you can echo '.$variables.' like this';

 

btw OLG, the first method is actually correct. You can echo variables inside doubles quotes, just not single without surrounding them with {}

<h3 align="center" class="style1"> Instructions </h3>
<div align="center"><span class="style1">
<div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br>
  The Date of Renewal must be as such - 01 01 2000
    <table width="400px" style="border" bgcolor="#808080">
    </span>
  </div>
</div>
<tr>
<td>
<P align="center">
<form action="" method="post">
<div align="center">
<div align="center"><span class="style1">Cost per month </span>
  <input type=text name=cpm>
    <br>
  <span class="style1">Date of Renewal:  Day</span>
  <input type=text maxlength=2 size=2 name=rday> 
    <span class="style1">Month</span>
  <input type=text maxlength=2 size=2 name=rmonth> 
    <span class="style1">Year</span>
  <input type=text maxlength=4 size=2 name=ryear>
  <br>
  
  <input type=submit name='calculate' value='Calculate'>
  <input type=reset name='reset' value='Reset'>
  
  <?php
	if (isset($_POST["calculate"]) && $cpm=='')
echo "Please enter a number into the Cost per month box";

$todaysdate = time();

if (isset($_POST["calculate"]) && $rday==''){
echo "Please enter a numberinto the Renewal Day Date.";}

if (isset($_POST["calculate"]) && $rmonth==''){
echo "Please enter a numberinto the Renewal Month Date.";}

if (isset($_POST["calculate"]) && $ryear==''){
echo "Please enter a numberinto the Renewal Year Date.";}

$costpermonth = $_POST[cpm];
$renewalday = $_POST[rday];
$renewalmonth = $_POST[rmonth];
$renewalyear = $_POST[ryear];

$renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0);





$timedifference = $renewalseconds - $todaysdate;


$VAT = (int)($costpermonth/100)*17.5;

$price = $costpermonth / $timedifference;

$totalprice = $price + $VAT;

//Output final calculations

echo"<br>";
echo"<br>";
echo"<br>";
print_r('$VAT, $price, $totalprice'); //COMMENT THIS OUT IF YOU DON'T WANT IT

if (isset($_POST["calculate"])) {echo "The total price is £$totalprice";}
echo"<br>";
if (isset($_POST["calculate"])) {echo "The price without VAT is £$price";}
echo"<br>";
if (isset($_POST["calculate"])) {echo "The VAT is £$VAT";}
echo"<br>";

?>
</div>

 

Try that.

 

OLG: Why say you don't do it? It's perfectly fine to!

Yes, i also knew this Yesideez, i regularly do it in MySQL statements.

You mean like this?

$query=mysql_query("SELECT data FROM table WHERE `field`='$variable'");

?

 

OLG: I've been programming for 26 years - properly. I think I know how to echo variables. There's nothing wrong with it.

ok, this is what i have now

 

</div>
<h3 align="center" class="style1"> Instructions </h3>
<div align="center"><span class="style1">
<div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br>
  The Date of Renewal must be as such - 01 01 2000
    <table width="400px" style="border" bgcolor="#808080">
    </span>
  </div>
</div>
<tr>
<td>
<P align="center">
<form method='post'>
<div align="center">
<div align="center"><span class="style1">Cost per month </span>
  <input type=text name=cpm>
    <br>
  <span class="style1">Date of Renewal:  Day</span>
  <input type=text maxlength=2 size=2 name=rday> 
    <span class="style1">Month</span>
  <input type=text maxlength=2 size=2 name=rmonth> 
    <span class="style1">Year</span>
  <input type=text maxlength=4 size=2 name=ryear>
  <br>
  
  <input type=submit name='calculate' value='Calculate'>
  <input type=reset name='reset' value='Reset'>
  
  <?php
	if (isset($_POST["calculate"]) && $cpm='')
echo "Please enter a number into the Cost per month box";

$todaysdate = time();

if (isset($_POST["calculate"]) && $rday=''){
echo "Please enter a numberinto the Renewal Day Date.";}

if (isset($_POST["calculate"]) && $rmonth=''){
echo "Please enter a numberinto the Renewal Month Date.";}

if (isset($_POST["calculate"]) && $ryear=''){
echo "Please enter a numberinto the Renewal Year Date.";}

$costpermonth = $_POST[cpm];
$renewalday = $_POST[rday];
$renewalmonth = $_POST[rmonth];
$renewalyear = $_POST[ryear];

$renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0);





$timedifference = $renewalseconds - $todaysdate;


$VAT = ($costpermonth/100)*17.5;

$price = $costpermonth / $timedifference;

$totalprice = $price + $VAT;

//Output final calculations

echo"<br>";
echo"<br>";
echo"<br>";

if (isset($_POST["calculate"])) {echo "The total price is $totalprice <br>";
if (isset($_POST["calculate"])) echo "The price without VAT is $price <br>";
if (isset($_POST["calculate"])) echo "The VAT is $VAT <br>";}

?>
</div>

 

It works fine except i get E-06 on the end of the second number. Also how do i round it up to a whole number as its going to be money as you can proberbly work out.

The second line is this command

if (isset($_POST["calculate"])) echo "The price without VAT is $price <br>";

 

And with the round up and roun down. I need it to work it out its self whether it needs to round it up or round it down. Do i do and if statement for this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.