PHP函数用于一个表单中的两个按钮和两个计算


PHP Functions for two buttons in a form and two calculations

我目前创建了一个带有两个不同按钮的表单,我希望这两个按钮都能执行不同的功能,第一个按钮是"财务支付"按钮,当按下该按钮时,我希望它将用户在文本框中输入的值除以12,然后回显"您的月付款将是结果"。然后对于第二个按钮,即"全额支付",它会将该值乘以0.2,然后返回"请支付结果的押金,并在下周支付剩余部分"

我自己也尝试过,但不完全明白该怎么做才能让它发挥作用,我已经阅读了一些php中的函数,但我是一个初学者,不太确定我在用它做什么,任何帮助都将不胜感激。

这是我目前拥有的表格代码。

<div id="form">
<form action="nissan350z.php" method="post">
   <center> <input type="text" name="percent" id="percent" />
  <input type="Submit"  value="Pay on Finance"> 
  <input type="Submit"  Value="Pay Full Amount">  </center>
</form>
  <input type="Submit"  name="Finance" value="Pay on Finance"> 
  <input type="Submit"  name="Full" Value="Pay Full Amount">

请注意,我添加了name属性。然后使用进行验证

if (isset($_POST['Finance'])){
 //User pressed Finance
} 
if (isset($_POST['Full'])){
 // User pressed Full
}

给您的提交按钮一个名称:

<input type="submit" name="pay_on_finance" value="Pay On Finance">
<input type="submit" name="pay_full_amount" value="pay full amount">

在你的nizzan350z.php上,你可以检查点击了哪个按钮

if (isset($_POST['pay_on_finance'])) {
    //Do Something
} else if (isset($_POST['pay_full_amount'])) {
    // Do something else
}

您可以使用php和javascript来做同样的事情。

修改您上面提到的当前文件中的HTML代码,

<input type="Submit"  name="Finance" value="Pay on Finance"> 
<input type="Submit"  name="Full" Value="Pay Full Amount">

在您的nissan350z.php文件中写入,

<?php
if (isset($_POST['Finance'])){
  $result=str_replace(",","",$_POST['percent'])/12;
  echo $result;
  echo "Your monthly payment will be result";
} 
if (isset($_POST['Full'])){
  $result=str_replace(",","",$_POST['precent'])*0.2;
  echo $result;
  echo "please pay the deposit of result and pay the rest next week";
}
?>