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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题