繁体   English   中英

如何从html表行获取ID?

[英]How to get the ID from the html table row?

我已经有一个只显示一个数据的表,单击一个按钮(编辑)后,我需要它来显示/显示模态数据库中的其他数据。 我的问题是(目前),我无法获取html表中每一行的ID。 (一旦我可以获取我认为可以获取其他数据的ID)

以下是显示该表的PHP代码:

<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted";
$result = mysql_query($query);

echo "<table class='table'>
          <thead>
              <th>JOB</th>
              <th>STATUS</th>
              <th>APPLICATIONS</th>
              <th>EDIT</th>
              <th>DELETE</th>
          </thead>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
              <th>" . $row['job_title'] . "</th>
              <td>" . $row['status'] . "</td>
              <td>" . $row['applications'] . "</td>
              <td><a data-toggle='modal' data-target='#myModal'>edit</a></td>
              <td><a href='#'>delete</a></td>                  
          </tr>";
}
echo "</table>";
mysql_close();
?>

我认为您是在谈论job_posted表的主键ID。 添加ID数据属性,在a标记,然后访问它的点击该ID。

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

用您的ID键替换$row['id']

例如,您的primary_key是id 您需要更改为此打开模式的href

    <tr>
        <th>" . $row['job_title'] . "</th>
        <td>" . $row['status'] . "</td>
        <td>" . $row['applications'] . "</td>
        <td><a data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a></td>
        <td><a href='#'>delete</a></td>                  
    </tr>";

而且我认为您也需要更改打开模式的方式。 首先为您的href添加类作为示例,我将使用.openeditmodal

所以你的href看起来像这样

<a class='openeditmodal' data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a>

然后添加此行以打开模式。

  $(".openeditmodal").click(function(){
    var job_posted_id = $(this).data('id');
    $("#myModal").modal("show");
});

试试这个代码。

在你的循环内

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr>
          <th>" . $row['job_title'] . "</th>
          <td>" . $row['status'] . "</td>
          <td>" . $row['applications'] . "</td>
          <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
          <td><a href='#'>delete</a></td>                  
      </tr>";
}

在您的脚本中

$('#myModal').on('show.bs.modal', function(e){
    var id= $(e.relatedTarget).data('id');
    console.log(id);

});

您可以在隐藏字段中获取ID。

'<input type="hidden" value =' . $row["id"] . ' >'

您需要创建主键,在数据库的第一栏中自动递增,即您的$ row [“ id”]

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

请检查以下代码。

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
        <th>" . $row['job_title'] . "</th><td>" . $row['status'] . "</td><td>" . $row['applications'] . "</td>
                    <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
                    <td><a href='#'>delete</a></td>                  
                    </tr>";
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM