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条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测