生成的按钮未获取所需数据

时间:2018-11-03 15:32:30

标签: javascript php

我创建的代码应该在按下与行一致的按钮“发送电子邮件”时获取行数据,即,如果我单击第一个“发送电子邮件”按钮,则应该获取除最后一行之外的第一行单元格值列,因为这是按钮所在的列。表是从数据库动态创建的,并且按钮是使用第4列的数据创建的。但这在onclick事件中给我一个错误

Uncaught TypeError: Cannot read property '0' of undefined
    at outputdata (bloodonation.php:161)
    at HTMLButtonElement.onclick (bloodonation.php:146)
outputdata @ bloodonation.php:161
onclick @ bloodonation.php:146

我使用tbl.rows.length-1从检索到的数据中排除了按钮。请帮我。我知道我想念东西

<?
$bloodqr=mysqli_query($link,$bloodquery);

            echo "<table id='tblId'>";
            echo "<tr><th>Firstname</th><th>Fathername</th><th>Lastname</th></tr> ";
            while($row=mysqli_fetch_assoc($bloodqr)){
                echo"<tr><td>";
                echo $row['donnerfirstname'];
                echo "</td><td>";
                echo $row['donnerfathername'];
                echo "</td><td>";
                echo $row['donnerlastname'];
                echo "</td><td>";?><html><button id="myBtn" onclick="outputdata()">Send Email</button></html> <?php 
                echo"</td></tr>";

            }
?>
 <!DOCTYPE html>
<html>
<head>
<script>
var tbl = document.getElementById("tblId");
var tblbutton = document.getElementById("myBtn");
function outputdata(){ 
    if (tbl != null) {
          for(var i = 1; i < tbl.rows.length-1; i++)
                {
                  //  tblbutton.onclick = function()
                  //  {
                         //rIndex = this.rowIndex;
                         var firstname= this.cells[0].innerHTML;
                         var fathername= this.cells[1].innerHTML;
                         var lastname= this.cells[2].innerHTML;
                         alert(firstname+fathername+lastname);
                   // };
                }
    }
}
</script> </head>
</html>

0 个答案:

没有答案