获取特定行的单元格数据

时间:2018-10-31 07:16:06

标签: javascript php html mysql

伙计们,我有这段代码可以从数据库中获取数据并将其发布到html表中。我想将与按钮对应的特定行或单元格数据发送到我使用过的段落元素中,一旦单击该按钮

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

但不起作用 这是我的代码

 <?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>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用类似这种方法。 首先,您应该在tr标签中设置一个唯一的ID。 您可以获取带有tr标签id的td标签的数据。

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

$("button").click(function(){
    console.log($(this).closest('tr').index()); // Get the value of tr tag id.
}
  • 您也可以尝试使用此link