dsz7121 2015-06-14 11:04
浏览 72
已采纳

无法使用jquery加载php文件并在文件外部访问变量

If I load my php file (Home.php) via Jquery, I can't seem to be able to access $var in my Home.php file. It loads succesfuly but I get this "undefined" error:

( ! ) Notice: Undefined variable: var in /media/sf_sandbox/linksup/view/Home.php

I have done this in this exact same order. I declare the variable:

index.php

<?php
    $var = "bar";
?>

I load the file Home.php with .load():

index.php

<div id="ContentContainer">
    <script>        
        $( "#ContentContainer" ).load( "view/Home.php" );
    </script>
</div>

I then try to get the value of $var in Home.php:

Home.php

<div class="row">
        <div class="col-md-12">
            <h2>Home</h2>
            <?php echo $var?>
        </div>
</div>
  • 写回答

1条回答 默认 最新

  • drwpbrv668670 2015-06-14 11:10
    关注

    Why you are using jquery for that purpose. PHP include_once() will do that easily.

    In Home.php do like below:-

    <?php include_once('index.php');?>
    <div class="row">
            <div class="col-md-12">
                <h2>Home</h2>
                <?php echo $var?>
            </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记