php - 如何解决未捕获的类型错误:PHP 中不支持的操作数类型....
问题描述
我创建了一个使用 PHP 中的 cookie 工作的表单。这就是表格的样子。
当我输入价格和数量的值时,我希望它们相乘并将结果显示在“总账单价值”文本框中。每次我输入价格和数量的值时,我都希望它们相加,这就是我使用 cookie 的原因。这是我的代码。
<html>
<head></head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$result=0;
if(isset($_POST["btnReset"]))
{
setcookie("result", $result);
}
elseif(isset($_POST["btnClick"]))
{
if(isset($_COOKIE["result"]))
{
$result=$_COOKIE["result"];
}
$result= $result + $_POST["Price"]*$_POST["Quantity"];
setcookie("result", $result);
}
else
{
if(isset($_COOKIE["result"]))
{
echo "something";
}
}
}
?>
<?php
if (isset($result)) {
$bill = $result;
} else {
$bill = '';
}
?>
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="btnClick" value="Add">
<input type="submit" name="btnReset" value="Clear">
<input type="submit" name="btnPrint" value="Print"><br>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">
</li>
</ul>
</form>
</body>
</html>
21号线是这个
$result= $result + $_POST["Price"]*$_POST["Quantity"];
有人知道我为什么会收到这个错误吗?
另一件事是,当我输入产品名称、价格和数量并获得总账单价值并对许多产品执行此操作并点击“打印”按钮时,我希望打印出所有产品及其总账单价值. 我该怎么做呢?
非常感谢任何帮助。提前致谢!
这就是我得到的
解决方案
我要做的第一件事是将您的 PHP 代码移到开始<html>
标记上方,这将有助于避免使用setcookie
(例如) 的警告。
我稍微重写了您的代码,重命名了按钮和值。它已经在 PHP 5.4.45 版本中进行了测试。
<?php
// Default result value
$result = 0;
if ( !empty( $_POST ) ) {
$cookie_key = 'result';
$action = !empty( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : false;
$result = !empty( $_COOKIE[ $cookie_key ] ) ? (float)$_COOKIE[ $cookie_key ] : $result;
// Add product
if ( $action === 'add' ) {
$price = !empty( $_POST[ 'Price' ] ) ? (float)$_POST[ 'Price' ] : 0;
$quantity = !empty( $_POST[ 'Quantity' ] ) ? (int)$_POST[ 'Quantity' ] : 0;
if ( $price && $quantity ) {
$result = $result + ( $price * $quantity );
}
}
// Clear result
else if ( $action === 'clear' ) {
$result = 0;
}
// Print
else if ( $action === 'print' ) {
// Handle print action
}
// Store value
setcookie( $cookie_key, $result );
}
?>
<html>
<head></head>
<body>
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="action" value="add">
<input type="submit" name="action" value="clear">
<input type="submit" name="action" value="print"><br>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $result; ?>">
</li>
</ul>
</form>
</body>
</html>
这是在行动:
推荐阅读
- reactjs - 反应本机导航/上下文 API
- php - Docker PHP71 curl 工作 PHP72 不工作
- c# - 带有复选框的 WPF 组合框和带有搜索字段的文本框在滚动时显示文本框
- node.js - 使用 Lex 运行时在 Node JS 中发布内容
- javascript - 如何将数据从javascript解析到nodejs,然后保存在mongodb中?
- css - Font Awesome 5 听未知图标
- python - Paramiko、Python、SSH、路由器登录的单元测试错误
- php - 如何将输入类型=“提交”替换为联系表 7 上的按钮?
- java - 如何从 Map 创建 Guava HashMultiset
- google-bigquery - 用于将行数据折叠成 JSON 或结构的 BigQuery 函数