在 html 中使用 php 函数

Using php function in html

需要一些帮助,我正在使用 Php 处理订单,我的功能是正确的,因为当我回显它时它工作正常。 但是我不知道如何在 table 上将函数正确地调用到我的 div 中。我希望能够显示数量乘以价格的结果。任何帮助将不胜感激。

<?
/*
print_r($_POST)
*/

$firstName =  $_POST["firstName"];
$lastName = $_POST["lastName"];
$phone = $_POST["phone"];
$email = $_POST["emailAddress"];

$appleQuantity = $_POST["apple"];
$baconQuantity = $_POST["bacon"];
$breadQuantity = $_POST["bread"];
$cheeseQuantity = $_POST["cheese"];
$eggsQuantity = $_POST["eggs"];
$hamQuantity = $_POST["ham"];
$milkQuantity = $_POST["milk"];

$priceApples = 10;
$priceBacon = 2.5;
$priceBread = 5;
$priceCheese = 5;
$priceEggs = 3.6;
$priceHame = 6;
$priceMilk = 4;



 function subTotal($incomingQuantity , $incomingPrice)
{
 return $incomingQuantity * $incomingPrice;
}

?>



 <div align="center"><? subTotal($incomingQuantity , $incomingPrice ) ?></div>

您的函数不执行任何输出,它只是返回值。你需要在某处回声。

例如

<div><?php echo subTotal(...) ?></div>

function subTotal(...) {
   echo $result
}

假设您必须在 div 内的文本框中显示输出,如

<input type="text" id="myText" >

那么你的函数可以是

function subTotal($incomingQuantity , $incomingPrice)
{
 var result = $incomingQuantity * $incomingPrice;

document.getElementById("myText").value = result;
}