【问题标题】:Creating onchange/oninput listener inside PHP在 PHP 中创建 onchange/oninput 监听器
【发布时间】:2019-04-20 22:01:06
【问题描述】:

我正在尝试在我的表中创建一个 onchange/oninput 侦听器,以将价格和订购数量相乘以显示在总成本列中。数据是通过 SQL 数据库引入的。 PHP 部分可能有我无法识别的错误。

<script type="text/javascript">             
function Multiply(f){
    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);
?>

<form action="#" method="post">
<tr>
<th> ID</th>
<th>Item Name</th>
<th>Suggested Quantity</th>
<th>Order Quantity</th>
<th>Total Cost ($)</th>

</tr>

<?php
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>";
}
    ?>

</table>                
</form>

【问题讨论】:

    标签: javascript php html onchange


    【解决方案1】:

    您通过其 ID 调用输入,但在您的表单中,您仅指示名称。另一件事是您将函数设置为接收计算中从未提及的变量/参数 (f)
    试试这个修复

    <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);
    ?>
    
    <form action="#" method="post">
    <tr>
    <th> ID</th>
    <th>Item Name</th>
    <th>Suggested Quantity</th>
    <th>Order Quantity</th>
    <th>Total Cost ($)</th>
    
    </tr>
    
    <?php
    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><input type='text' id='price' value=" . $row['Price_item'] . "/></td>"; ?>
        <td><input type='text' id='editedvalues[]' value="<?php echo $row['suggested_qty']; ?>" oninput="Multiply()"/></td>
       <?php echo "<td><input type='text' id='result'/></td>";
        echo "</tr>";
    }
        ?>
    
    </table>                
    </form>
    

    【讨论】:

    • 谢谢。这确实解决了在乘法函数中引用正确 ID 的问题。但是,我在调用 Multiply 函数的 echo 行中遇到错误。有没有办法在没有语法错误的情况下使用 oninput 调用?
    • @group15 更新了我的答案。你可以那个并检查它是否有效
    • 我在乘法行中仍然收到错误消息“unexpected
    • 仍然没有运气,得到同样的错误。是否有另一种方法将 javascript 函数合并到 php 行中?
    • 没有更多错误! :) 一切都来了,似乎无法得到输出的总成本,只是输出一个空白字段。非常感谢您的帮助!
    【解决方案2】:

    我猜你会收到一个unexpected &lt; 错误,因为虽然你的复制粘贴 sn-p 中缺少开始标签 &lt;?php,但它可能存在于你的实际代码中,并且你正在直接编写您的 javascript sn-p 没有将其包含在回声中。或者,否则将其移出 php 标签 &lt;?php ?&gt;。您还有额外的 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>
    

    【讨论】:

    • 这很好用!唯一的问题是,使用“复杂卷曲语法建议”的乘法函数不起作用?有什么建议吗?
    • curly 语法是 PHP 的东西;如果这就是你的意思,它不适用于 Javascript。
    • 是的,我能够使用 curly 语法并获得输出。使用您发布的最终代码,我无法使其正常工作。有没有办法将 curly 语法与 javascript 函数结合使用?
    • 是的,转义双引号:\" --- 我已经更新了我的答案来演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多