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

javascript - Alert in PHP not working inside if - Stack Overflow

programmeradmin6浏览0评论

I want to put an alert inside an if (in a php document) with other actions. The other actions works fine but not the alert. Can anybody help me?. This is where the alert is:

if(empty($response)){ 
    //SOME ACTIONS
    echo '<script type='text/javascript'> alert("Message´s 1st line\nMessage´s 2nd line");</script>';
    //SOME ACTIONS
    header('Location: SOMEWEBSITE');                                
}else{ 
   //ANOTHER ACTION
} 

All actions works fine except for the alert. I tried on different browsers but the alert doesnt appear.

Sorry for my english

Thank you all.

I want to put an alert inside an if (in a php document) with other actions. The other actions works fine but not the alert. Can anybody help me?. This is where the alert is:

if(empty($response)){ 
    //SOME ACTIONS
    echo '<script type='text/javascript'> alert("Message´s 1st line\nMessage´s 2nd line");</script>';
    //SOME ACTIONS
    header('Location: SOMEWEBSITE');                                
}else{ 
   //ANOTHER ACTION
} 

All actions works fine except for the alert. I tried on different browsers but the alert doesnt appear.

Sorry for my english

Thank you all.

Share Improve this question asked Mar 20, 2016 at 4:15 user3822492user3822492 1451 silver badge11 bronze badges 2
  • The SO syntax highlighting should give you a clue as to your issue. – Sumner Evans Commented Mar 20, 2016 at 4:26
  • Check your inverted mas type='text/javascript' should be doubles type="text/javascript"and you might want language="javascript" If the apostrophes were not backticks you would need to escape those Message\'s like that. Usually you want exit; after the location redirect to stop the rest of the page loading. – Steve Commented Mar 20, 2016 at 6:45
Add a ment  | 

5 Answers 5

Reset to default 2

It would be better if you use below mentioned code.

if(empty($response)){ 
//SOME ACTIONS
?>
 <script type="text/javascript"> alert("Message´s 1st line\nMessage´s 2nd line");</script>
<?php
//SOME ACTIONS
header('Location: SOMEWEBSITE');                                
}else{ 
//ANOTHER ACTION
} 

First thing is to fix quotes.

Second thing is remove header() call

http://php/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

And don't use location header for 200 response

https://en.wikipedia/wiki/HTTP_location

Yes fix your quote.

The bellow code should work which you tried if you don't use redirection.

echo '<script language="javascript">alert("message");</script>';  

You can't expect alert where you redirecting to another page from php. If you use javascript redirection, then it can be work. Replace redirection line with something like

echo '<script language="javascript">window.location = "http://SOMEWEBSITE.COM";</script>';  

Try This echo '<script type="text/javascript">alert("Message´s 1st line\nMessage´s 2nd line");</script>'

echo "<script type='text/javascript'> alert('Message´s 1st line\nMessage´s 2nd line');</script>";

The code that you wrote the echo was terminating after

type='
发布评论

评论列表(0)

  1. 暂无评论