error with arlert function that I created

Asked

Viewed 49 times

3

Galera set up a very simple function of Alert in php. It works however this presenting a problem.

Whenever I call him that:

Alert("Pedido finalizado");

It works, but when I put an n to have a break if line, it doesn’t work. Example:

Alert("Pedido \n finalizado");

Does anyone know what it might be? in the browser the only error that appears and this:

SyntaxError: Unexpected EOF

Good follows the function:

function Alert($msn){
?>
<script>
    alert('<?=$msn?>');
</script>
<?php
}
  • it worked like this. but I didn’t understand why

  • 2

    When you use \n in the variable, it already interprets scape, and when it passes to Alert, it is no longer there. Using two, it interprets one, and the other Alert.

  • OK thanks so much for the help and removal

  • 1

    But, another thing, I don’t know much about php, but this <script> shouldn’t come in one ècho something like this, I think the EOF error refers to this. Or not? (taking doubt :P )

  • It works in echo tbm, but so the code gets much more organized. And the issue of EOF was n

  • Ah ok, all right. Thank you :)

Show 1 more comment

1 answer

3


That’s what’s going on:

<script>
    alert('Pedido
finalizado');
</script>

To solve this the \ would have to be escaped with a \.

Alert("Pedido \\n finalizado");

And so:

<script>
    alert('Pedido\nfinalizado');
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.