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?

sarnold
105k23 gold badges187 silver badges243 bronze badges
asked Mar 9, 2012 at 9:46
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") ?>'); 
answered Mar 9, 2012 at 9:48
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.

answered Mar 9, 2012 at 9:49

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>;'; ?>
answered Mar 9, 2012 at 9:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.