简体   繁体   中英

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"

<?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() .

<?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. (or add the event using addEventListener() in JavaScript)

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

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"

This is correct: "k \\" is a double quote"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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