【发布时间】:2021-03-06 08:29:09
【问题描述】:
我尝试做一些事情来列出我数据库中的所有主题,然后在它们旁边有一个按钮允许我删除或更新主题内的问题。所以我添加了带有唯一参数的 onclick 函数。但是当我点击删除按钮时它没有显示任何消息。请帮助我。
<!DOCTYPE html>
<?php
require "connectPHP.php";
// to get the total number of topic present
$counter = "SELECT COUNT(IdTopic) FROM TOPIC";
$result = mysqli_query($con, $counter);
if($row = mysqli_fetch_assoc($result)){
$totalTopic = $row['COUNT(IdTopic)'][0];
}
$totalTopic = (int)$totalTopic;
?>
<head>
<link rel="stylesheet" href="mystyle.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<br>
<div class="collection">
<br>
<div class = "collectionContainer">
<br>
<div>
<?php
require "connectPHP.php";
$selectDataFromTopic = "SELECT * FROM TOPIC ORDER BY LENGTH(IdTopic), IdTopic";
$result = mysqli_query($con,$selectDataFromTopic); // query
echo "<table id='topicTable'>"; // start a table tag in the HTML
echo "<tr class='tableHeader'>";
echo "<th style='width:10%;'>IdTopic</th>";
echo "<th style='width:15%'>Sub topic</th>";
echo "<th style='witdh:55%'>title</th>";
echo "<th style='witdh:10%'>Delete</th>";
echo "<th style='witdh:10%'>change</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
//$row['index'] the index here is a field name
echo "<tr>";
echo "<td>" . $row['IdTopic'] . "</td>";
echo "<td>" . $row['subTopic'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
$D_position = "D".$row['IdTopic'];
echo "<td><button type='submit' class='deleteButton' id='".$D_position."' onclick='delete('".$D_position."')'> Delete</button></td>";
$C_position = "C".$row['IdTopic'];
echo "<td><button type='submit' class='changeButton' id='".$C_position."' onclick='change('".$C_position."')'> Change question </button></td>";
echo "</tr>";
}
echo "</table>"; //Close the table in HTML
?>
</div>
<script>
function delete(deletePositon){
confirm( "It will be deleted");
}
function change(changePosition){
// add some code here
}
</script>
</div>
<br>
</div>
</body>
</html>
【问题讨论】:
-
这显然与 PHP 无关。请点击
edit,然后点击[<>]sn-p 编辑器并发布一个minimal reproducible example,其中仅包含渲染的HTML 和脚本。没有 PHP -
看来问题出在引用上。您正在生成
onclick='delete('D100')'。这被视为 3 个单独的属性onclick='delete('、D100和')' -
@mplungjan PHP 肯定是问题的一部分,因为它与他们如何将变量替换到 HTML 中有关。
-
当他们发布渲染的 HTML 时,很明显存在引用问题。无论如何要摆脱它,请参阅我的答案
-
@mplungjan 是的,但是您将看不到需要修复的问题的根源。
标签: javascript php html dom onclick