[英]write php inside javascript alert
I write PHP inside JS in the following way 我用以下方式在JS中编写PHP
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. echo__("Error-login")
与xml相关联,用symfony翻译成两种语言,但现在它不起作用。
How do I fix this? 我该如何解决?
您在alert()
调用中缺少引号。
alert('<?php echo __("Error-login") ?>');
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. 因此,在将其传递给Javascript之前,您需要转义单引号。
You must name file as. 您必须将文件命名为。 Php.
PHP的。 Because JavaScript doesn't support php code as inside.
因为JavaScript不支持内部的PHP代码。
[javatpoints ][1]
尝试这个
<?php echo '<script language="javascript">confirm("Do you want this?")</script>;'; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.