javascript - 如何使用 php 从 html 行(在表中)获取数据

标签 javascript php html mysql

我试图在有人点击我的动态表时获取信息,这样我就可以逐列阅读并提取数据,然后将其显示在文本框中。不知道该怎么做,因为我是这种语言的新手,希望您能提供帮助。我现在正在做的是从我的数据库中提取数据并用它创建动态表。 我已经尝试了几件事,但都没有奏效,所以我不会在这里粘贴所有失败的尝试。 这是我的代码(因为很多人对它是 mysql 有疑问,所以我将它更新为 Mysqli_:

    if (($result)||(mysqli_errno == 0))
{
 echo "<table cellspacing='0' cellpadding='0' border='0' width='126%'>
 <tr>
  <td>
   </table>
 <div style='width:450; height:350px; overflow:auto;'>
 <table cellspacing='0' cellpadding='1' border='1' width='380px'>
   <tr style='color:white;background-colorgrey'>";
 if (mysqli_num_rows($result)>0)
 {
      //loop thru the field names to print the correct headers
      $i = 0;
      while ($i < mysqli_num_fields($result))
      {
          $fetch = mysqli_fetch_field_direct($result, $i);
          echo "<th>". $fetch->name . "</th>";
          $i++;
  }
 //display the data
 while ($rows = mysqli_fetch_assoc($result))
 {
  echo "<tr>";
  foreach ($rows as $data)
  {
    echo "<td align='center' width='400px'>". $data . "</td>";
  }
  echo"</tr>";
}
 }else{
   echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
 }
   echo "</table></div>";                

最佳答案

对于任何对答案感兴趣的人,我已经弄清楚了(变量“id”存储列“0”中的元素,但您可以更改 0 以获得不同的列):

        <table cellspacing='0' cellpadding='0' border='0' width='126%'>
  <tr>
  <td>
   </table>
 <div style='width:450; height:350px; overflow:auto;'>
 <table style="display: table-row" class='prueba' cellspacing='0'              cellpadding='1' border='1' width='380px'>
    <tbody>
    <tr style='color:white;background-colorgrey'>

   <?php

   ///******///
  ///CONECTION AND QUERY INFO WOULD GO HERE///
  ///******///

  if (($result)||(mysqli_errno == 0))
 {

  if (mysqli_num_rows($result)>0)
  {
      //loop thru the field names to print the correct headers
      $i = 0;
      echo "<th><a>Check</a></th>";
      while ($i < mysqli_num_fields($result))
      {
          $fetch = mysqli_fetch_field_direct($result, $i);
   echo "<th><a>". $fetch->name . "</a></th>";
   $i++;
  }
   //display the data
   while ($rows = mysqli_fetch_assoc($result))
   {
   echo "<tr onClick='Content(this)'>";
   foreach ($rows as $data)
   {
    echo "<td align='center' width='400px'><a>". $data . "</a></td>";
  }
  echo"</tr>";
  }
  }/*else{
   echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
     }*/

  }else{
   echo "'Error in running query :'. mysqli_error()";
   }    

  //echo "<style>";

 /*echo "</tr></table></td></tr><tr><td>
      <table min-width='400px' cellspacing='0' cellpadding='1'       border='1'>";*/

 echo "</table></div>";
 echo "<script>
    function Content(elem){
        elem.style.backgroundColor = 'red';

        var id = $(elem).find('td:eq(0)').text();
        alert(id);}
        </script>";
  ?>
  </p>

关于javascript - 如何使用 php 从 html 行(在表中)获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300788/

相关文章:

javascript - AWS 从客户端上传文件到 S3

PHP 文件处理和 fread 错误

php - 当值存在时事件记录省略插入

php - Paypal(沙盒/模拟器)IPN 返回无效,尽管付款成功?

html - Bootstrap 导航栏更改高度

javascript - 如何根据表单数据重定向操作 ="phpmailer.php"?

javascript - Meteor:Template.rendered 回调函数中尚不存在 DOM 元素

javascript - 遍历数组并将每个值除以 100

java - 使用 '<tr onclick=alertContents()>' 从 HTML 表中获取一行内容

jquery - YouTube IFrame API 播放/停止不起作用