douxian1939 2015-08-24 17:02
浏览 54
已采纳

使用HTML表格将JavaScript添加到PHP文件

I have a php file with php code that fetches data from mysql table and displays it in a table. The table contains delete links. To prevent accidental deletion, I added a javascript alert to the delete link. But the javascript does not show the alert dialog box.

below is the picture of the page enter image description here

From the picture above , the user is expected to be prompted when they click the delete link.

below is my code.

<? php
include('/templates/header.php');
$host = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "datacentre"; // Database name 
$tbl_name = "data_centre_users"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if ($con - > connect_error) {
  die("Connection failed: ".$con - > connect_error);
}

// Check connection
if ($con - > connect_error) {
  die("Connection failed: ".$conn - > connect_error);
}

$sql = "SELECT * FROM $tbl_name WHERE status <> 'approved' ";
$result = $con - > query($sql);


?>

< section id = "sidebar" >

  < /section>

<section id="content">

<div id="scroll-table">
<table >
<caption>
           List data from mysql
            </caption >
  < tr >
  < th class = "center" > < strong > Name < /strong></th >
  < th class = "center" > < strong > Request < /strong></th >
  < th class = "center" > < strong > Purpose < /strong></th >
  < th class = "center" > < strong > Description < /strong></th >
  < th class = "center" > < strong > Approve < /strong></th >
  < th class = "center" > < strong > Delete < /strong></th >
  < /tr>
            <?php
            if($result->num_rows > 0){
                / / output data of each row
while ($rows = $result - > fetch_assoc()) { ?>
  < tr >
    < td class = "center" > <? php echo $rows['first_name']; ?> < /td>
                        <td class="center"><?php echo $rows['request']; ?></td >
  < td class = "center" > <? php echo $rows['purpose']; ?> < /td>
                        <td class="center"><?php echo $rows['description']; ?></td >
  < td class = "center" > < a href = "approve.php?id=<?php echo $rows['id']; ?>"
  onclick = "return confirm("
  Ae u sure ");" > approve < /a></td >
    < td class = "center" > < a href = "delete.php?id=<?php echo $rows['id']; ?>"
  onclick = "return confirm('Are you sure you want to delete');" > delete < /a></td >
    < /tr>

                    <?php
                }
            }
            ?> 
</table >
    < /div>
</section >

<?php
$con->close();

include('/templates / footer.php ');
?>
  • 写回答

1条回答 默认 最新

  • dszm02606009 2015-08-25 10:28
    关注

    Use:

    <a href="javascript:if(confirm('Are you sure you want to delete?')){ location.href='delete.php?id=<?php echo $rows['id']; ?>'; }">delete</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function