dongmu5815 2016-03-11 06:50
浏览 56
已采纳

在PHP中调用自定义JS警报

I use a custom alert javascript. I want to call this javascript inside a php. I got a basic alert to work but I cannot get the custom one to work.

This code works. It opens an alert window if my file path is empty

    <?php

$albums_json_file1 = site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . 'main.json';
 if (!empty($albums_json_file1))

 echo "<script type='text/javascript'>alert('message')</script>"
 ?>

This code does not work

    <?php

$albums_json_file1 = site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . 'main.json';
if (!empty($albums_json_file1))

echo "<script> swal('REGISTRATION SUCCESS!', 'You are now being redirected to the payment screen!', 'success'); </script>"
  ?>

In my webpage I have the script source for the custom alert and if I just add this code in my webpage the popup alert works BUT I need it to check the site path and return the alert only if it is empty.

Here is the regular script. If I put this in the body of my webpage the alert pops up.

<script>

swal("REGISTRATION SUCCESS!", "You are now being redirected to the     payment screen!", "success");

</script>
  • 写回答

2条回答 默认 最新

  • dongzhenge2014 2016-03-11 06:56
    关注

    Your html is loading top to bottom.

    Move the declaration of swal() to the header so it can be used anywhere in the body.

    So your PHP echo is probably before you declare swal()

    example:

    <script>swal('Does not work');</script>
    <script>
    var swal = function(txt){
        alert(txt);
    }
    </script>
    <script>swal('Works');</script>
    

    The reason alert(); works is because that works in your entire Document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部