我猜你会收到一个unexpected < 错误,因为虽然你的复制粘贴 sn-p 中缺少开始标签 <?php,但它可能存在于你的实际代码中,并且你正在直接编写您的 javascript sn-p 没有将其包含在回声中。或者,否则将其移出 php 标签 <?php ?>。您还有额外的 javascript 结束标记。
代替:
<?php
<script type="text/javascript">
function Multiply(){
var myBox1 = document.getElementById('editedvalues[]').value;
var myBox2 = document.getElementById('price').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2;
result.value = myResult;
}
}
</script>
$sql = "SELECT item_price.item_id, item_price.ITEM_NAME,suggested_qty,Price_item
FROM item_price JOIN suggested_item
ON item_price.ITEM_NAME = suggested_item.ITEM_NAME";
$result = $conn->query($sql);
?>
试试这个:
<script type="text/javascript">
function Multiply() {
var myBox1 = document.getElementById('editedvalues[]').value;
var myBox2 = document.getElementById('price').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2;
result.value = myResult;
}
</script>
<?php
$sql = "SELECT item_price.item_id,
item_price.ITEM_NAME,
suggested_qty,
Price_item
FROM item_price
JOIN suggested_item ON item_price.ITEM_NAME = suggested_item.ITEM_NAME;";
$result = $conn->query($sql);
?>
此外,我想向您介绍complex curly syntax {} 的字符串表示方法。为了代码的可读性,它通常可以更容易地像这样在花括号之间引用变量; 注意:只适用于双引号之间。
while ($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>{$row['item_id']}</td>";
echo "<td>{$row['ITEM_NAME']}</td>";
echo "<td>{$row['suggested_qty']}</td>";
echo "<td>{$row['Price_item']}</td>";
echo "<td><input type='text' name='editedvalues[]' value='{$row['suggested_qty']}' oninput='Multiply()' /></td>";
echo "<td><input name='result' /></td>";
echo "</tr>";
}
在查看了您正在尝试做的事情之后,我首先建议您将两种语言混合使用并不是一种好的做法。尽管如此,让我们顺其自然吧。有一件关键的事情你还没有解决。您正在使用静态 ID 引用,但您(可能)输出大量行。不幸的是,您最终会针对相同的输入运行您的 javascript,而不是我想象的您尝试定位的行。
您需要将它们设为动态引用,或者,只需对它们进行唯一命名。以下是如何解决此问题的示例:
<?php
# the SQL query
$sql = "SELECT item_price.[item_id] as itmId,
item_price.[ITEM_NAME] as itmName,
[suggested_qty] as itmQty,
[Price_item] as itmPrice
FROM item_price
JOIN suggested_item ON item_price.ITEM_NAME = suggested_item.ITEM_NAME;";
# execute the query
$query = $conn->query($sql);
$items = $query->result();
?>
<script type="text/javascript">
function Multiply(itemId) {
var itemQty = document.getElementById('itemQty'+itemId).value;
var itemPrice = parseInt(document.getElementById('itemPrice'+itemId).innerText);
var cost = itemQty * itemPrice;
document.getElementById('itemCost'+itemId).innerText = cost;
}
</script>
<form action="#" method="post">
<table>
<thead>
<tr>
<th>Item ID</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Suggested Quantity</th>
<th>Order Quantity</th>
<th>Total Cost ($)</th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo $item->itmId; # item id ?></td>
<td><?php echo $item->itmName; # item name ?></td>
<td id="itemPrice<?php echo $item->itmId; ?>"><?php echo $item->itmPrice; # item price ?></td>
<td><?php echo $item->itmQty; # suggested qty ?></td>
<td><input type="text"
id="itemQty<?php echo $item->itmId; ?>"
value="<?php echo $item->itmQty; ?>"
oninput="Multiply('<?php echo $item->itmId; ?>')"></td>
<td id="itemCost<?php echo $item->itmId; ?>"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
更新:
当然,你可以像这样使用 curly 语法:
<tbody>
<?php
foreach ($items as $item) {
echo "<tr>
<td>{$item->itmId}</td>
<td>{$item->itmName}</td>
<td id='itemPrice{$item->itmId}'>{$item->itmPrice}</td>
<td>{$item->itmQty}</td>
<td><input type='text'
id='itemQty{$item->itmId}'
value='{$item->itmQty}'
oninput='Multiply(\"{$item->itmId}\")'></td>
<td id='itemCost{$item->itmId}'></td>
</tr>";
}
?>
</tbody>