2

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?

2
  • add ; in '<?php echo __("Error-login"); ?>' 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. Commented Mar 3, 2015 at 12:23

3 Answers 3

4

You are missing quotes in the alert() call.

alert('<?php echo __("Error-login") ?>'); 
Sign up to request clarification or add additional context in comments.

Comments

2

Your line becomes

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.

1 Comment

it does not work the same alert('<?php echo __("Error-login"); ?>')
1

try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.