PHP变量中的Onclick函数

onclick function in php variable

本文关键字:Onclick 函数 变量 PHP      更新时间:2023-09-26

如何在包含php变量的<a>标签上添加onClick事件

$delete = "<a onclick='return confirmDelete('deleterecord.php?chkid=$sl&table=$getid&path=$validURL');'><img src='images/dustbin.png' width='43px' height='30px'></a>";  // link not pass

//脚本
<script>
    function confirmDelete(delUrl) {
        if (confirm("Are you sure you want to delete")) {
            window.location = delUrl;
        }
    }
</script>

1。$link = "deleterecord.php?chkid=$sl&table=$getid&path=$validURL";

和使用

return confirmDelete('"<?=$link?>'")

2。在单引号前使用黑斜杠转义单引号:

  <img src=''images/dustbin.png'' width=''43px'' height=''30px''>

使用php的echo返回html/css/js的内容:http://php.net/manual/de/function.echo.php

<?php
$delete = "
    <a onclick='confirm_('"deleterecord.php?chkid=$sl&table=$getid&path=$validURL''")'; class='link'>
        <img src='images/dustbin.png' width='43px' height='30px'>
    </a>";
echo($delete);
?>
<script>
function confirm_(url){
        console.log(url);
        if (confirm("Are you sure you want to delete")){
            window.location = url;
        }
}
</script>

就像Rayon Dabre说的,你必须转义字符串。


Maurize的休闲/智能/懒惰解决方案:

您应该用delete.php?action=delete&id=2

这样的路径回显链接

和PHP端

if (isset($_GET["action"]) && $_GET["action"] == "delete"){ echo $_GET["id"]; }

将看起来像:

<?php
if (isset($_GET["action"]) && $_GET["action"] == "delete"){
    echo $_GET["id"];
}
?>
<a href="index.php?action=delete&id=1">Lazy Casual?</a>

<?php
echo("
<a href='deleterecord.php?chkid=$sl&table=$getid&path=$validURL'>
    <img src='images/dustbin.png' width='43px' height='30px'>
</a>
");
?>

经过测试,像墨西哥人一样工作。

    删除的杰作将使用AJAX + JS - Maurize