dongmu5815 2016-03-11 14: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 14: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条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿