admin管理员组

文章数量:1331886

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?

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?

Share Improve this question edited Mar 9, 2012 at 10:21 sarnold 104k23 gold badges185 silver badges243 bronze badges asked Mar 9, 2012 at 9:46 SimonSimon 372 silver badges6 bronze badges 2
  • add ; in '<?php echo __("Error-login"); ?>' – mgraph Commented Mar 9, 2012 at 9:48
  • I hope php code in js works, only if the corresponding file name is saved as a .php file. – shasi kanth Commented Mar 3, 2015 at 12:23
Add a ment  | 

3 Answers 3

Reset to default 4

You are missing quotes in the alert() call.

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

Your line bees

alert(Error-login);

As you can see, you are missing the quotes:

alert('Error-login');

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.

try this

<?php echo '<script language="javascript">confirm("Do you want this?")</script>;'; ?>

本文标签: write php inside javascript alertStack Overflow