获取特定行的单元格数据

Hey guys I have this code that gets data from my database and posts it to an html table. I want to send a particular row or cell data corresponding to the button,once the button is clicked, to a paragraph element I used this

console.log(this.childNodes[0].innerHTML); // column 1 in row1  

but it doesn't work Here is my code

 <?php
    if(isset($_POST['searchbox'])){    
        $bloodonation =$_POST['searchbox'];
        $multiple= explode(',',$bloodonation);
        $var1 = $multiple[0]; // firstname                                                                                                   
        $var2 = $multiple[1]; // fathername
        $var3 = $multiple[2]; // lastname                /*bloodtype.blood_type='$var4' AND bodytype.bodytype='$var5' AND */
        $_SESSION["firstname"] = $var1;
        $_SESSION["fathername"] = $var2;    
        $_SESSION["lastname"] = $var3;  
        if(!empty($bloodonation)){
            //$myfile = fopen("file.txt", "w");
            //file_put_contents('file.txt',$bloodonation);
            //fclose($myfile);
            $bloodquery ="SELECT personprofile.firstname,personprofile.fathername,personprofile.lastname,bloodtype.blood_type,bodytype.bodytype,personprofile.bloodoner,personprofile.organdoner
                        FROM personprofile,bloodtype,bodytype
                        WHERE   personprofile.bloodtype= bloodtype.id AND 
                                personprofile.hascancer ='No' AND personprofile.chronicdisease= 'No' AND personprofile.autoimmunedisease= 'No' AND
                                personprofile.bodytype= bodytype.id";

            //$sql = "SELECT `firstname`, `fathername`, `lastname` FROM `personprofile` WHERE chronicdisease=\"No\" AND hascancer=\"No\" AND autoimmunedisease=\"No\"";
            $bloodqr=mysqli_query($link,$bloodquery);

            echo "<table>";
            echo "<tr><th>Firstname</th><th>Fathername</th><th>Lastname</th><th>Blood type</th><th>Body type</th></tr> ";
            while($row=mysqli_fetch_assoc($bloodqr)){
                echo"<tr><td>";
                echo $row['firstname'];
                echo "</td><td>";
                echo $row['fathername'];
                echo "</td><td>";
                echo $row['lastname'];
                echo "</td><td>";
                echo $row['blood_type'];
                echo "</td><td>";
                echo $row['bodytype'];
                echo "</td><td>";?><html><button onclick="outputdata()">Send Email</button></html> <?php 
                echo"</td></tr>";

            }
        } 
    } ?>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var paragraphdata = document.getElementById("demo");

function outputdata() {
        console.log(this.childNodes[0].innerHTML); // col1
        console.log(this.childNodes[1].innerHTML); // col2
        paragraphdata.innerHTML = "Geolocation is not supported by this browser.";
    }
</script>
</body>
</html>

You can try to use like this method. Firstly you should set an unique id in the tr tag. And you can get data of the td tag with the tr tag id.

echo '<tr id="'.$unique_id.'">';

$("button").click(function(){
    console.log($(this).closest('tr').index()); // Get the value of tr tag id.
}
  • And also you can try to use this link