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 doublestype="text/javascript"
and you might wantlanguage="javascript"
If the apostrophes were not backticks you would need to escape thoseMessage\'s
like that. Usually you wantexit;
after the location redirect to stop the rest of the page loading. – Steve Commented Mar 20, 2016 at 6:45
5 Answers
Reset to default 2It 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='