I’m generating a string in PHP and then eventually passing this string into a JavaScript alert box, my problem is I actually can’t add line breaks in my alert box.
My code looks as follows
JavaScript
x
13
13
1
$str = "This is a stringn";
2
$alert = $str."This is the second line";
3
4
if(!empty($alert)){
5
?>
6
<script type="text/javascript">
7
$(document).ready(function() {
8
alert('<?=$alert?>');
9
});
10
</script>
11
<?php
12
}
13
I’m getting the error:
Undeterminnated string literal
If I remove the n
from the string it works 100% but without line breaks.
Advertisement
Answer
This happens because PHP interprets the n before JavaScript has the chance to, resulting in a real line break inside the Javascript code. Try
JavaScript
1
2
1
\n
2