繁体   English   中英

第二个Javascript OnClick事件不起作用-未定义function()

[英]Second Javascript OnClick Event Not Working - function() is not defined

注意:我不能使用JQuery,我需要在javascript,php,sql,html和css之间混合使用。

我想做的是能够单击客户编号,并显示一个div,其中显示单个客户的电话号码,销售代表姓名和信用额度。

我实现此方法的方式是使用PHP,同时循环从SQL数据库中获取数据并将记录输出到表中。 通过onclick事件,我将额外的表实现为函数($ orderText)。

我遇到的功能是第二张表的customerDetails()。 第一个完美地工作。 我在css样式表中填充了样式的div,以便固定位置。

    <?php
    require_once "dbconfig.php";

    try {            
    $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=UTF8", 
    $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";



    $sql = 'SELECT DISTINCT
    O.orderDate, O.customerNumber, O.comments, O.status, O.orderNumber,
    OD.productCode, P.productLine, P.productName
    FROM orders O
    JOIN orderdetails OD on O.orderNumber = OD.orderNumber
    JOIN products P ON OD.productCode = P.productCode
    WHERE O.status = "In Process"
    ORDER BY O.orderNumber';


    $q = $conn->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC);

    $sql2 = 'SELECT
    O.orderNumber, O.orderDate, O.customerNumber, O.comments, O.status, C.phone, C.salesRepEmployeeNumber as repNumber, C.creditLimit, concat(E.firstName, " ", E.lastName) as repName, E.employeeNumber
    FROM orders O
    JOIN customers C on O.customerNumber = C.customerNumber
    JOIN employees E on C.salesRepEmployeeNumber = E.employeeNumber
    ORDER BY O.orderDate desc LIMIT 20';


    $q2 = $conn->query($sql2);
    $q2->setFetchMode(PDO::FETCH_ASSOC);    



}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();

}
?>

<!doctype html>

<html>
    <head>
        <title>Page Title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1.0">
        <link rel="stylesheet" href='orders.css'>

    </head>

    <body>
        <div>
            <div class="jsbox" id="orderDetails" ></div>




                <table>
                <thead>
                    <tr>    
                        <th>Order Number</th>
                        <th>Order Date</th>
                        <th>Status</th>
                        <th>Customer Number</th>


                    </tr>
                </thead>  
                <tbody>
                    <?php while ($r = $q->fetch()): ?>

                    <?php   $text =  "<table><tr><th>Product Code</th><th>Product Line</th><th>Product Name</th><th>Comments</th>";
                            $text .= "<tr><td>" . htmlspecialchars($r['productCode']) . "</td>";
                            $text .= "<td>" .  htmlspecialchars($r['productLine']) . "</td>";
                            $text .= "<td>" .  htmlspecialchars($r['productName']) . "</td>";
                            $text .= "<td>" .  htmlspecialchars($r['comments']) . "</td></table>" ?>
                    <tr>
                        <td ><?php echo "<button onclick=\"myFunction('$text')\">" . htmlspecialchars($r['orderNumber']) . "</button>" ?></td>
                        <td><?php echo htmlspecialchars($r['orderDate']) ?></td>

                        <td><?php echo htmlspecialchars($r['status']) ?></td>
                        <td><?php echo htmlspecialchars($r['customerNumber']) ?></td>
                    </tr>
                <?php endwhile; ?>




                </tbody>                    
            </table>

        </div>
<script type="text/javascript">
    function myFunction(msg) {
        document.getElementById("orderDetails").innerHTML = msg;
}
        </script>
<div>
            <div class="jsbox"  id="customerOrderDetails" ></div>

                <table>
                <thead>
                    <tr>    
                        <th>Order Number</th>
                        <th>Order Date</th>
                        <th>Status</th>
                        <th>Customer Number</th>


                    </tr>
                </thead>  
                <tbody>
                    <?php while ($s = $q2->fetch()): ?>
                    <tr>

                    <?php   $orderText =  "<table><tr><th>Phone Number</th><th>Rep Name</th><th>Credit Limit</th>";
                            $orderText .= "<tr><td>" . htmlspecialchars($s['phone']) . "</td>";
                            $orderText .= "<td>" .  htmlspecialchars($s['repName']) . "</td>";
                            $orderText .= "<td>" .  htmlspecialchars($s['creditLimit']) . "</td></tr></table>"; ?>
                         </tr>
                    <tr>
                        <td><?php echo htmlspecialchars($s['orderNumber']) ?></td>
                        <td><?php echo htmlspecialchars($s['orderDate']) ?></td>

                        <td><?php echo htmlspecialchars($s['status']) ?></td>
                        <td><?php echo "<button onclick=\"customerDetails('$orderText')\">" . htmlspecialchars($s['customerNumber']) . "</button>" ?></td>


                </tr>
                <?php endwhile; ?>




                </tbody>                    
            </table>


        </div>

  <script type="text/javascript">
    function customerOrder(msg) {
        document.getElementById("customerDetails").innerHTML = msg;
    }   
    </script>


</body>
</html>

所以我需要修复的区域是

<div>
            <div class="jsbox"  id="customerOrderDetails" ></div>

                <table>
                <thead>
                    <tr>    
                        <th>Order Number</th>
                        <th>Order Date</th>
                        <th>Status</th>
                        <th>Customer Number</th>

                    </tr>
                </thead>  
                <tbody>
                    <?php while ($s = $q2->fetch()): ?>
                    <tr>

                    <?php   $orderText =  "<table><tr><th>Phone Number</th><th>Rep Name</th><th>Credit Limit</th>";
                            $orderText .= "<tr><td>" . htmlspecialchars($s['phone']) . "</td>";
                            $orderText .= "<td>" .  htmlspecialchars($s['repName']) . "</td>";
                            $orderText .= "<td>" .  htmlspecialchars($s['creditLimit']) . "</td></tr></table>"; ?>
                         </tr>
                    <tr>
                        <td><?php echo htmlspecialchars($s['orderNumber']) ?></td>
                        <td><?php echo htmlspecialchars($s['orderDate']) ?></td>

                        <td><?php echo htmlspecialchars($s['status']) ?></td>
                        <td><?php echo "<button onclick=\"customerDetails('$orderText')\">" . htmlspecialchars($s['customerNumber']) . "</button>" ?></td>


                </tr>
                <?php endwhile; ?>  
                </tbody>                    
            </table>
 </div>

  <script type="text/javascript">
    function customerOrder(msg) {
        document.getElementById("customerDetails").innerHTML = msg;
    }   
    </script>

你忘了分号。 您也可以在不使用转义字符的情况下执行此操作,只需编写即可:

<?php echo "<button onclick='customerDetails(".$orderText.");'>"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM