[英]Extract data from a table created by a loop (PHP)
I have the following problem: I'm creating an HTML table with a PHP for-loop. 我有以下问题:我正在用PHP for循环创建HTML表。 There is data and a button in each row.
每行中都有一个数据和一个按钮。 When the user presses the button (id="detail"), the "id"-field of the corresponding row (id="patID") is supposed to be stored in a PHP variable.
当用户按下按钮(id =“ detail”)时,应将相应行(id =“ patID”)的“ id”字段存储在PHP变量中。 Every attempt I have made failed because javascript simply takes the first element on the page with the id "patID" and (to my knowledge) I don't have a way to select this element in PHP.
我所做的每一次尝试均以失败告终,因为javascript仅采用ID为“ patID”的页面上的第一个元素,而且(据我所知)我没有办法在PHP中选择此元素。 This is my code:
这是我的代码:
<?php
if (isset($_POST['search']))
{
//irrelevant details of MySQL PDO-connection omitted
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0)
{ ?>
<h2>Ergebnisse</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Geburtstag</th>
<th>Klasse/Kurs</th>
<th>Vorerkrankungen</th>
<th>Allergien</th>
<th>Anmerkung</th>
<th>Aktualisiert</th>
<th> Optionen</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row)
{ ?>
<tr id="row">
<td id="patID"><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["firstName"]); ?></td>
<td><?php echo escape($row["lastName"]); ?></td>
<td><?php echo escape($row["birthday"]); ?></td>
<td><?php echo escape($row["course"]); ?></td>
<td><?php echo escape($row["preIllnesses"]); ?></td>
<td><?php echo escape($row["allergies"]); ?> </td>
<td><?php echo escape($row["note"]); ?> </td>
<td><?php echo escape($row["created"]); ?> </td>
<td>
<button id="detail">Mehr</button>
</td>
</tr>
<? } ?>
</tbody>
</table>
<?php
}
else
{ ?>
<blockquote><b>Keine Ergebnisse gefunden.</b></blockquote>
<?php
}
}
?>
The 'id' attribute can only be given once to an html-element. 'id'属性只能给html元素一次。 You should you a class instead:
您应该改为上课:
<button class="detail">Mehr</button>
You should store the id in an extra attribute if you want to grab it later in javascript. 如果您想稍后在javascript中获取ID,则应将ID存储在一个额外的属性中。 For example, if you are using jQuery, you can use a data attribute:
例如,如果您使用的是jQuery,则可以使用数据属性:
<button class="detail" data-id="some_id">Mehr</button>
<script>
$('.detail').click(function () {
var id = $(this).data('id');
// do something with id
});
</script>
This will add a click-listener to all elements with the 'detail' class. 这将为所有带有'detail'类的元素添加一个点击侦听器。 So if a user clicks on an element having that class , it will execute the given function with
this
pointing to the clicked element. 因此,如果用户点击一个元素具有类,它将执行给定函数
this
指向点击的元素。 And since that element has the 'data-id' attribute, we can use jQuery's data function to get the contents of that attribute. 并且由于该元素具有'data-id'属性,因此我们可以使用jQuery的data函数来获取该属性的内容。
Your way if you change ID to class 如果您将ID更改为班级,您的方式
<tr class="row">
<td class="patID"><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["firstName"]); ?></td>
<td><?php echo escape($row["lastName"]); ?></td>
<td><?php echo escape($row["birthday"]); ?></td>
<td><?php echo escape($row["course"]); ?></td>
<td><?php echo escape($row["preIllnesses"]); ?></td>
<td><?php echo escape($row["allergies"]); ?> </td>
<td><?php echo escape($row["note"]); ?> </td>
<td><?php echo escape($row["created"]); ?> </td>
<td>
<button class="detail">Mehr</button>
</td>
</tr>
<script>
$(function() {
$('.detail').click(function(){
// var id = $(this).parents('.row').find('.patID').html();
alert($(this).parents('.row').find('.patID').html());
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.