php 中的 JavaScript 函数可显示更多点击信息(数据库查询)

标签 javascript php mysql html

以下代码连接到保存客户信息的本地数据库。首先,该页面应该显示客户名称和国家/地区,但单击名称后应显示该特定客户的更多信息。

下面这段代码的问题是,无论单击哪个名称,它都只会显示第一个客户的信息。谁能看出我哪里可能出错了?我需要为每个按钮(客户名称)创建一个 ID 吗?请记住,数据库中有很多客户,我觉得每个客户的 ID 很难实现,尤其是在数据库被修改的情况下。

 <DOCTYPE html!>
 <html>
    <head>
    <link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
    <div class="nav">
        <?php include 'nav.php';
        ?>
    </div>
    <?php include 'dbconfig.php';
    $sql ='SELECT * FROM `customers` ORDER BY `customers`.`country`    ASC';
    $q = $conn->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC); 
    ?>
    <div class = "main">
        <table>
            <?php while ($r = $q->fetch()): ?>
                 <tr>
                     <td><button onclick = "showCustomer();"/><?php echo ($r['customerName'])?></td>
                     <td><?php echo ($r['country'])?></td>
                </tr>
                <tr id ="extra" style="display:none">
                     <td><?php echo ($r['customerNumber'])?></td>
                     <td><?php echo ($r['contactLastName'])?></td>
                     <td ><?php echo ($r['contactFirstName'])?></td>
                     <td><?php echo ($r['phone'])?></td>
                     <td><?php echo ($r['addressLine1'])?></td>
                     <td><?php echo ($r['addressLine2'])?></td>
                     <td><?php echo ($r['city'])?></td>
                     <td><?php echo ($r['state'])?></td>
                     <td><?php echo ($r['postalCode'])?></td>
                     <td><?php echo ($r['salesRepEmployeeNumber'])?>   </td>
                     <td><?php echo ($r['creditLimit'])?></td>
                 </tr>
             <?php endwhile; ?>
        </table>
       </div>

        <div class="foot">
        <?php include 'foot.php'; ?>
       </div>
       <script>



        function showCustomer() {
            var showForm=document.getElementById('extra');
            if (showForm.style.display="none") {

               showForm.style.display ="";
                alert('ok');
                console.log(showForm);
            }
            else if (showForm.style.display=""){
                showForm.style.display ="none";
            }
        }
    </script>
</body>
</html>
</DOCTYPE>

最佳答案

你所有的<tr>有相同的id="extra" 。按照建议,您需要为每个人提供不同的“id”,这就是方法:我创建了一个变量 $i (第 18 行),在 while(第 20 行)之后立即增加,作为参数插入到 showCustomer 的调用中(第23行),插入到id“extra”的末尾(第27行),最后,在底部,其中函数showCustomer声明后,参数“index”被插入到id“extra”的末尾。

<DOCTYPE html!>
 <html>
    <head>
    <link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
    <div class="nav">
        <?php include 'nav.php';
        ?>
    </div>
    <?php include 'dbconfig.php';
    $sql ='SELECT * FROM `customers` ORDER BY `customers`.`country`    ASC';
    $q = $conn->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC); 
    ?>
    <div class = "main">
        <table>
            <?php $i = 0;
                  while ($r = $q->fetch()):
                    $i++;           // UNIQUE INDEX FOR EACH CUSTOMER.
            ?>
                 <tr>
                     <td><button onclick = "showCustomer('<?php echo $i;?>');"/>
                          <?php echo ($r['customerName'])?></td>
                     <td><?php echo ($r['country'])?></td>
                </tr>
                <tr id ="extra<?php echo $i;?>" style="display:none">
                     <td><?php echo ($r['customerNumber'])?></td>
                     <td><?php echo ($r['contactLastName'])?></td>
                     <td ><?php echo ($r['contactFirstName'])?></td>
                     <td><?php echo ($r['phone'])?></td>
                     <td><?php echo ($r['addressLine1'])?></td>
                     <td><?php echo ($r['addressLine2'])?></td>
                     <td><?php echo ($r['city'])?></td>
                     <td><?php echo ($r['state'])?></td>
                     <td><?php echo ($r['postalCode'])?></td>
                     <td><?php echo ($r['salesRepEmployeeNumber'])?>   </td>
                     <td><?php echo ($r['creditLimit'])?></td>
                 </tr>
             <?php endwhile; ?>
        </table>
       </div>

        <div class="foot">
        <?php include 'foot.php'; ?>
       </div>
       <script>



        function showCustomer( index ) { // UNIQUE INDEX AS PARAMETER.
            var showForm=document.getElementById('extra' + index); // USING INDEX.
            if (showForm.style.display="none") {

               showForm.style.display ="";
                alert('ok');
                console.log(showForm);
            }
            else if (showForm.style.display=""){
                showForm.style.display ="none";
            }
        }
    </script>
</body>
</html>
</DOCTYPE>

关于php 中的 JavaScript 函数可显示更多点击信息(数据库查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36725402/

相关文章:

php - mysql & php - 如何计算多个日期范围的总经验年数?

php - 表单数据正在提交,但不会进入 mySQL

mysql - 如何通过一次连接从表中提取多个项目?

javascript - 运行nodejs v10时可以使用 "import"关键字吗?

php - 无法在实时服务器中发布富文本编辑器内容?

javascript - 下拉值不发布

php - 索引页脚很古怪

javascript - Codeigniter:以管理员用户身份登录并以员工用户身份登录

javascript - 迭代具有动态类名的元素时,JQuery .each 循环不起作用

javascript - DataTables 无法添加行