duancong7573 2018-06-24 07:55
浏览 64
已采纳

将PHP变量回送到JavaScript变量中会在HTML中打印PHP变量

I have a HTML page with a script tag with PHP in it as follows:

<div id="myName">
<script type="text/javascript">
   var myName = <?php
   if(isset($_SESSION['name']) && $_SESSION['name'] != ''){
       echo $_SESSION['name'];
   }
   else
       echo "NotSet";
   ?>;
document.write(myName);
</script>
</div>

But running this actually prints like below:

<div id="myName">
    var myName=SomeName;
    document.write(myName);
</div>

Expected:

<div id="myName">
    SomeName
</div>

On doing inspect element:

<div id="myName">
<script type="text/javascript">
    "var myName=SomeName;
    document.write(myName);"
</script>
</div>

I want to post-process the variable myName and derive other values ahead in JS logic. So, I need to get the php value in JS variable.

Please note: I am not asking how to get a PHP variable into JS variable (for which there are many different questions on Stack Overflow). This is a different question and hence not a duplicate for those questions.

  • 写回答

4条回答 默认 最新

  • dongzhong8691 2018-06-24 08:21
    关注

    You could assign the PHP data as a JavaSscript variable like this - it will be available for further client side processing as desired and you can update the DOM to reflect the content using standard methods.

    <html>
        <head>
            <title>php & js vars</title>
        </head>
        <body>
        <div id='myName'>
            <script>
                <?php
                    printf( 'var myName="%s";', !empty( $_SESSION['name'] ) ? $_SESSION['name'] : 'NotSet' );
                ?>
                document.getElementById('myName').innerHTML=myName;
                alert( myName );// var still available...
            </script>
        </div>
        </body>
    </html>
    

    Update

    The above snippet was written in haste with little thought given to the various ways in which it could break, but to accommodate for double quotes you could make use of htmlentities

    <?php
        session_start();
        $_SESSION['name']='Geronimo"s Revenge';
    ?>
    <html>
        <head>
            <title>php & js vars</title>
        </head>
        <body>
        <div id='myName'>
            <script>
                <?php
                    printf( 'var myName="%s";', !empty( $_SESSION['name'] ) ? htmlentities( $_SESSION['name'], ENT_QUOTES   ) : 'NotSet' );
                ?>
                document.getElementById('myName').innerHTML=myName;
            </script>
        </div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?