php 里面的 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9889648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:08:25  来源:igfitidea点击:

javascript inside php

phpjavascriptjquerycodeigniter

提问by Faryal Khan

How can i write a javascript alert inside php ? For example if i want to popup a javascript alert in this if condition how I can do it ?

如何在 php 中编写 javascript 警报?例如,如果我想在这种情况下弹出一个 javascript 警报,我该怎么做?

if(isset($_POST['downloadkey'])){
                $key = $_POST['downloadkey'];
                if($key != $data['download_key']){
                    echo 'Your key is wrong.';
                    echo '<script type="text/javascript"> alert(Your downlaod key is wrong, Please try again!);</script>';
                }
            }

Thanks in advance

提前致谢

回答by Rinto George

You forgot to wrap string with quotes in alert .

您忘记在 alert 中用引号将字符串括起来。

"Your downlaod key is wrong, Please try again!" is a string

“您的下载密钥错误,请重试!” 是一个字符串

if(isset($_POST['downloadkey'])){
                $key = $_POST['downloadkey'];
                if($key != $data['download_key']){
                    echo 'Your key is wrong.';
                    echo '<script type="text/javascript"> alert("Your downlaod key is wrong, Please try again!");</script>';
                }
            }

回答by Manuel

You can do this by putting this alert into the onload of the body like so:

您可以通过将此警报放入主体的加载中来做到这一点,如下所示:

<body onload="alert('Your download key is wrong, Please try again!');">

...


</body>