我已经创建了一个php页面,它将创建一个表单并向自己发布信息。然后我用它创建一个对象,我通过一个方法运行,我想在这里返回/回显一个字符串' s我的代码。
<?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>
如果它转到另一个页面,我不介意,但理想情况下我希望它能回显到页面上。我没有从中获得任何错误,我对PHP中的OOP不是很有经验。
修改我已尝试$result = $user->CalcSave();
echo $result;
和
echo $user->CalcSave();
但它重新加载没有
的同一页面?action=depreciation
在页面的末尾,没有任何回应。
答案 0 :(得分:0)
试试这个:
$result = $user->CalcSave();
echo $result;
和
$result = $user->CalcDeprec();
echo $result;