通过循环增加td的id并在函数中获取id

问题描述 投票:-1回答:1

我正在填充一个包含数据库数据的表,我想获得td的值,即row1,在PHP函数中执行操作,我不知道该怎么做,

    <?php
             $sql = "SELECT DISTINCT name,c_no,email,speciality FROM doctor_temp";
    $result = mysqli_query($con,$sql);
                   while ($row = mysqli_fetch_array($result))



                {?>
                    <?php $count=1;
                            $count++;?>
                   <tr>
                   <td><?php echo $row[0];?></td>
                   <td id=<?php echo $count;?>><?php echo $row[1];?></td>
                   <td><?php echo $row[2];?></td>
                   <td><?php echo $row[3];?></td>
                   <td><button type="button" class="btn btn-success" onclick="insert()">register</button></td>

                   </tr>
              <?php  } ?>
            </tbody>
          </table>
        </div>
 <script type="text/javascript">
        function insert()
        {
           var a = document.getElementById($count).innerHTML;
           alert(a)
        }
    </script>
    </body>

click on the register button to get corresponding number

php html
1个回答
0
投票

小心!您的代码是错误的,因为您需要输入ID号而不是PHP变量。如果你想使用count,你必须将它放在一个PHP标签中并回显它,但是你将JS与PHP代码混合在一起。

getElementById(<?php echo $count; ?>).innerHTML

请在调试时查看控制台日志,这样可以使事情变得更加容易,从而使您无需提出可以自行回答的问题。

我建议您不要预先处理JS,这对于任何编程语言来说都是非常糟糕的做法。

© www.soinside.com 2019 - 2024. All rights reserved.