私はフォームを作成し、それ自身に情報を投稿するPHPページを作成しました。私はそれから私が返す/私はコードをここに文字列をエコーしたいメソッドを実行するオブジェクトを作成します。メソッドphpの値をエコーしようとしています
<?php
include_once 'checkLogin.php';
$action='';//this will store what the user wants to do
if(isset($_GET['savings'])){$action='save';}
if(isset($_GET['depreciation'])){$action='deprec';}
$time=$_POST['time'];//get the values from the form below
$interest=$_POST['interest'];
$InitialSum=$_POST['InitialSum'];
//depreciation and savings can be done on the same page
class Information{
function __construct($time,$interest,$InitialSum){
$this->time=$time;
$this->interest=$interest;
$this->InitialSum=$InitialSum;
}
function CalcSave(){
$totalAmt=$InitialSum*($interest*100);//the total amount, this will count as one year
for ($x=0;$x<$time;$x++){//therefore this should only do 1 less than the total
$totalAmt=($interest*100)*$totalAmmt;//after another year
}
return'After '.$time.' years your savings will be worth '.$totalAmt.'.';
}
function CalcDeprec(){
$totalAmt=$InitialSum*$interest;
for ($x=0;$x<$time;$x++){//therefore this should only do 1 less than the total
$totalAmt=$interest*$totalAmmt;//after another year
}
return 'After'.$time.'years your asset will be worth '.$totalAmt.'.';
}
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
Initial Amount: <input type="number" name="InitialSum"><br>
Interest: <input type="number" name="interest"><br>
How long: <input type='number' name='time'><br>
<input type="submit">
</form>
<p>
<?php
$user=new Information($time,$interest,$InitialSum);//this creates the user object
if ($action=='save'){
$user->CalcSave();//this will run the method since user is the object
echo $result;
}
if ($action=='deprec'){
$user->CalcDeprec();
echo $result;
}
?>
</p>
</body>
</html>
、それは別のページに行く場合、私は気にしませんが、理想的に私はそれがページ上にエコーされたいです。私はそれから何かエラーを取得していないと私は非常にOOPのPHPで経験はありません。 編集私は$result = $user->CalcSave(); echo $result;
と
echo $user->CalcSave();
を試みたが、それはページの最後に
?action=depreciation
せずに同じページをリロードし、何もエコーされません。
'するecho $ user-> CalcSave();'と 'するecho $ user-> CalcDeprecを();' – RiggsFolly