简体   繁体   English

无法在php print中获得JavaScript变量的正确语法

[英]Can't get right syntax for JavaScript variables inside php print

What is wrong with the following code, that I produced for testing the content of the button? 我为测试按钮内容而生成的以下代码有什么问题?

I get the syntax error "unexpected T_CONSTANT_ENCAPSED_STRING" 我得到语法错误“意外T_CONSTANT_ENCAPSED_STRING”

<?php /* Created on: 14-9-2013 */ ?>
<html>
<head>
<script type="text/javascript" charset="utf-8"> 
var mldkr = Math.round(screen.availWidth/2.08);
var supro = Math.round((screen.availHeight)/6), alto=supro*4
var urlesp = "http://translate.google.com/#<?php echo $fontLingvo ?>|eo|<?php echo $fontVorto ?>";
    </script>

</head>
<body>
<table>
<?php
print '
    <tr height="30">
        <td></td>
        <td></td>
        <td></td>                      
    </tr>
    <tr>                                                    
        <td align="right">
            <button type="button" onclick="fenEspo = window.open(urlesp, '', 'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height='+alto+', top='+supro+', left='+mldkr+', width='+mldkr, true)" title="Ne gravas, ne kalkuli&#285;as por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
        </td>
        <td align="center">
            <input type="radio" name="eo" value="1" />jes 
            <input type="radio" name="eo" value="0" />ne 
            <input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
        </td>
    </tr>                                                   
    '
?>
</table>
</body>
</html>

Thank you! 谢谢!

If you want to use ' inside of ' , you got to escape them. 如果你想使用'内部' ,你必须逃避它们。

// "usual" string
$foo = 'abc';
// string with ' as content
$bar = ' abc \' def ';

In your example, have a look at the call to window.open() . 在您的示例中,查看对window.open()的调用。

<?php
print '
    <tr height="30">
        <td></td>
        <td></td>
        <td></td>                      
    </tr>
    <tr>                                                    
        <td align="right">
            <button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height='+alto+', top='+supro+', left='+mldkr+', width='+mldkr+', true)" title="Ne gravas, ne kalkuli&#285;as por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
        </td>
        <td align="center">
            <input type="radio" name="eo" value="1" />jes 
            <input type="radio" name="eo" value="0" />ne 
            <input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
        </td>
    </tr>                                                   
    ';
?>

For a cleaner code base you should move that code to a separate function in your already existing <script> tag and just reference this in the button. 为了更清晰的代码库,您应该将该代码移动到现有<script>标记中的单独函数中,并在按钮中引用它。 (or add the event using addEventListener() in JavaScript) (或在JavaScript中使用addEventListener()添加事件)

Your quotes are not properly escaped. 您的报价未正确转义。 There are several ways to fix this. 有几种方法可以解决这个问题。

Heredoc: 定界符:

print <<<EOT
<tr height="30">
  <td></td>
  <td></td>
  <td></td>                      
</tr>
<tr>                                                    
  <td align="right">
    <button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height=\'+alto+\', top=\'+supro+\', left=\'+mldkr+\', width=\'+mldkr, true)" title="Ne gravas, ne kalkuli&#285;as por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
  </td>
  <td align="center">
    <input type="radio" name="eo" value="1" />jes 
    <input type="radio" name="eo" value="0" />ne 
    <input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
  </td>
</tr> 
EOT;

Escaping: 转义:

print '
<tr height="30">
    <td></td>
    <td></td>
    <td></td>                      
</tr>
<tr>                                                    
    <td align="right">
        <button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height=\'+alto+\', top=\'+supro+\', left=\'+mldkr+\', width=\'+mldkr, true)" title="Ne gravas, ne kalkuli&#285;as por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
    </td>
    <td align="center">
        <input type="radio" name="eo" value="1" />jes 
        <input type="radio" name="eo" value="0" />ne 
        <input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
    </td>
</tr>                                                   
';

you have an extra ' before toolbar=0 'toolbar=0 你有一个额外的' toolbar=0 'toolbar=0

remove and it should be fine 删除它应该没问题

You need to escape the double quote as: \\" instead of "" 您需要将双引号转义为: \\"而不是""

An un-escaped " will prematurely terminate the string. 未逃脱的"将过早地终止字符串。

Example: 例:

This is incorrect: "k " is a double quote" 这是不正确的: "k " is a double quote"

This is correct: "k \\" is a double quote" 这是正确的: "k \\" is a double quote"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM