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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?