write php inside javascript alert

I write PHP inside JS in the following way

alert(<?php echo __("Error-login") ?>);

echo__("Error-login") correlates with an xml to translate in two languages ​​with symfony, but now it does not work.

How do I fix this?

Asked By: Simon

Answer #1:

You are missing quotes in the alert() call.

alert('<?php echo __("Error-login") ?>'); 
Answered By: code_burgar

Answer #2:

Your line becomes


As you can see, you are missing the quotes:


If somebody uses quotes in the translation, this will also generate an error:

alert('Error's are here');

So you need to escape single quotes before you pass it to Javascript.

Answered By: Sjoerd

Answer #3:

try this

<?php echo '<script language="javascript">confirm("Do you want this?")</script>;'; ?>
Answered By: rejo
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

# More Articles