使用PHP加载Javascript

编辑:我的功能被破坏,其中包含/ n.感谢woofmeow让我质疑我的功能

我是编程新手.我一直在努力,过去几个月我学到了很多东西(这里有一堆!)但我很难过.

我有一个PHP脚本从另一个PHP脚本调用,我无法获得实际运行的代码.它曾经运行,然后我改变了一些东西,并没有保存更改,我不知道我做了什么.我知道,我知道,新手的错误(我从中学到了!). Javascript在视图页面源上显示正常,但不再运行.

这是页面代码,可能就是这么简单:

<script type="text/javascript">
function delete_user(user_id) 
{if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!")) {window.location = "delete_user.PHP?user_id=" + user_id;}}</script>

这是PHP
show_users脚本将其发送到查看器:

$delete_user_script = <<<EOD
function delete_user(user_id) 
{
    if (confirm("Are you sure you want to delete this user?" 
                + "\nThere's really no going back!")) 
    {
        window.location = "delete_user.PHP?user_id=" + user_id;
    }
}
EOD;

HTML中的PHP在工作时没有改变:

<?PHP 
while ($user = MysqL_fetch_array($result)) 
{
    $user_row = sprintf("<li><a href='show_user.PHP?user_id=%d'>%s %s</a>(<a href='mailto:%s'>%s</a>)<a href='javascript:delete_user(%d);'><img class='delete_user' src='../images/delete.png' width='15' /></a></li>",$user['user_id'],$user['first_name'],$user['last_name'],$user['email'],$user['user_id']);

    echo $user_row;
}
?>

最后,查看脚本为我们提供了页面代码(注意:$embedded_javascript是$delete_user_script):

if (!is_null($embedded_javascript)) 
{
    echo '<script type="text/javascript">' . $embedded_javascript . '</script>';
}

当我将鼠标悬停在图像上以删除用户时,它仍会显示正确的脚本链接(“Javascript:delete_user(%d)”,其中%d是user_id)但是它就像没有定义函数一样,没有任何反应.任何想法都非常感谢!谢谢!

基本上你的if语句是错误的(即使在$delete_user_script变量中).由于它是从一个新的线开始,口译员将假设一个;在它的最后,因此你的if语句中断.

你的功能有这个

if (confirm("Are you sure you want to delete this user?" 
                + "\nThere's really no going back!"))

应该是这样的

if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))

有时它只是一个小小的错误.希望有帮助:)

编辑1:这是javascript和PHP代码中的原始发布问题中的错误.现在它已被编辑,以反映if是不是打破了.编辑2:我被告知这有助于解决问题所以我会在这里保留这个问题.希望它也能帮助别人.PS:如果有人想删除,请告诉我.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...