【发布时间】:2019-10-01 14:51:54
【问题描述】:
我在表格中显示公告,我想让每一行都可点击,这有点像我用 js onclick 函数所做的那样(我只能点击名称,但我想将其应用于整行)。现在我想在新标签中查看有关公告的详细信息。我的问题从这里开始。我想在单击一行时获得公告 ID,但不知道该怎么做。另外我正在用js做onclick,其余的是php,所以那里有点乱。我知道这不是最好的方法,所以我需要你关于如何解决这个问题的建议。
附:我对js不是很熟悉,那些js部分取自网络教程。下面的代码是我显示搜索结果的部分。
<section id="results">
<div class="container">
<table>
<tr>
<a href="#"><th>Name</th></a>
<th>Where</th>
<th>From</th>
<th>Date</th>
<th>Price</th>
<th>Type</th>
<th>PId</th>
</tr>
<?php
if(isset($_POST['search_btn'])){
include_once "db_connect.php";
$from = $_POST['from'];
$where = $_POST['where'];
$date = $_POST['date'];
$type = $_POST['type'];
$procid = $_POST['proc_id'];
if(empty($from) || empty($where) || empty($date) || empty($type)){
header("Location: index.php?search=empty");
exit();
}else{
$sql = "SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";
$query = mysqli_query($conn, $sql);
$num_rows = mysqli_num_rows($query);
while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
echo "<tr><td onclick='content(this)'>" . $rows['p_name'] . " " . $rows['p_surname']. " </td><td>" .$rows['p_from']. "</td><td>" .$rows['p_where']. "</td><td>" .$rows['p_date']. "</td><td>" .$rows['price']. "</td><td>" .$rows['type']. "</td></tr>" ;
}
echo "</table>";
}
}else{
echo "Error!";
}
?>
<?php
$fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($fullUrl, "search=empty") == true){
echo "<p class='error'>You did not fill in all fields!</p>";
}
?>
</table>
</div>
</section>
<script>
function content(elem){
<?php
?>
window.open('procdetail.php');
}
</script>
【问题讨论】:
-
负责打开东西的代码在哪里?为什么不将该 ID 写入标记并使用数据属性读取它?
-
@NicoHaase 底部的 js 脚本实际上在新选项卡中打开了 procdetail.php。我只是不清楚如何从表中获取数据。我的意思是我希望能够单击一行中的任意位置并转到新标签中的公告详细信息。
-
@MagsudHajiyev 您正在将数据添加到
<td>标记上,如果您希望函数针对整行执行,只需将其添加到<tr>标记上。如果有帮助,请查看我的回答。