doubi9531 2017-08-06 22:27
浏览 29
已采纳

在JavaScript代码中转义<?php?

I have to echo the below JavaScript code with using only one <?php tag. How to escape <?php inside the echo?

<script type="text/javascript">

function drawLatestTen() {
    var data = google.visualization.arrayToDataTable(<?php echo json_encode( $json ); ?>);

    var ac = new google.visualization.ComboChart(document.getElementById('ipt_dolt_ten_stat'));
    ac.draw(data, {
        title : '<?php _e( 'Last 30 days form submission statistics', 'ipt_dolt' ); ?>',
        height : 300,
        vAxis : {title : '<?php _e( 'Submission Hits', 'ipt_dolt' ) ?>'},
        hAxis : {title : '<?php _e( 'Date', 'ipt_dolt' ); ?>'},
        seriesType : 'bars',
        series : {<?php echo count( $json[0] ) - 2; ?> : {type : 'line'}},
        legend : {position : 'top'},
        tooltip : {isHTML : true}
    });
}

</script>

so the result is:

<?php echo '<script type="text/javascript">

function drawLatestTen() {
    var data = google.visualization.arrayToDataTable(<?php echo json_encode( $json ); ?>);

    var ac = new google.visualization.ComboChart(document.getElementById(\'ipt_dolt_ten_stat\'));
    ac.draw(data, {
        title : \'<?php _e( \'Last 30 days form submission statistics\', \'ipt_dolt\' ); ?>\',
        height : 300,
        vAxis : {title : \'<?php _e( \'Submission Hits\', \'ipt_dolt\' ) ?>'},
        hAxis : {title : \'<?php _e( \'Date\', \'ipt_dolt\' ); ?>\'},
        seriesType : \'bars\',
        series : {<?php echo count( $json[0] ) - 2; ?> : {type : \'line\'}},
        legend : {position : \'top\'},
        tooltip : {isHTML : true}
    }); ?>
}

</script>'; ?>
  • 写回答

2条回答 默认 最新

  • doucong1853 2017-08-06 22:39
    关注

    Mixing php / js can be a bit confusing because the quotes can intersect.

    Notice that we're using " and '. You might want to echo with " or with ' depending on what you need to accomplish. echoing with " in php allows you to use variables in the string, ie: echo "hello {$username}" the lamdas ({}) aren't required, but many devs prefer them.

    Here's an incomplete example to get you started. You just echo it all at once and concatenate with . for strings in php.

    <?php 
    $title = translate( 'Submission Hits', 'ipt_dolt' );
    $date = translate( 'Date', 'ipt_dolt' );
    echo '
    <script type="text/javascript">
    
    function drawLatestTen() {
        var data = google.visualization.arrayToDataTable( '. json_encode( $json ) .' );
    
        var ac = new google.visualization.ComboChart(document.getElementById('ipt_dolt_ten_stat'));
        ac.draw(data, {
            title : '<?php _e( 'Last 30 days form submission statistics', 'ipt_dolt' ); ?>',
            height : 300,
            vAxis : {title : "'. $tite .'"},
            hAxis : {title : "'.$date.'"}, // notice two quotes, double to denote a string in js and singles to break/concatenate the php output. (that's where things get tricky.)
            seriesType : 'bars',
            series : { '. (count( $json[0] ) - 2) .' : {type : 'line'}},
            legend : {position : 'top'},
            tooltip : {isHTML : true}
        });
    }
    </script>'; ?>
    

    Edit: Special thanks to @wh1t3h4ck5 & @jh1711 for helping me tweak this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog