Simon
Simon

Reputation: 37

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?

Upvotes: 2

Views: 4946

Answers (3)

rejo
rejo

Reputation: 3350

try this

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

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75609

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.

Upvotes: 2

code_burgar
code_burgar

Reputation: 12323

You are missing quotes in the alert() call.

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

Upvotes: 4

Related Questions