最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript inside php - Stack Overflow

programmeradmin2浏览0评论

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 ?

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

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 ?

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

Share Improve this question asked Mar 27, 2012 at 12:37 Faryal KhanFaryal Khan 8694 gold badges19 silver badges37 bronze badges 10
  • why is this snippet not working? on a roughly base the alert should appear if both if conditions are true. Do you render these two lines inside a valid html page? – Daxcode Commented Mar 27, 2012 at 12:39
  • 4 Does the code you posted work? What happens when you run it? (There is a typo in the code, first of all. The string in the alert() needs to be wrapped in quotes.) – David Commented Mar 27, 2012 at 12:39
  • tough typo, haven't even seen it (credits should go to David! :)) – Daxcode Commented Mar 27, 2012 at 12:43
  • 2 Why do you want to invoke JavaScript when you are already on the server side? – Amberlamps Commented Mar 27, 2012 at 12:44
  • @Daxcode: Of course, if the OP simply runs the code, an error message would point out the same thing :) – David Commented Mar 27, 2012 at 12:45
 |  Show 5 more ments

2 Answers 2

Reset to default 5

You forgot to wrap string with quotes in 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>';
                }
            }

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>
发布评论

评论列表(0)

  1. 暂无评论