3

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>
2
  • Why would you want to load a php file from a JS file? More likely you mean that you want to call a URL which happens to point to a resource delivered by PHP routine? But then, the resulting resource is not a PHP file but a rendered HTML for example, or JSON, or HTML that contains JS elements, or a plain JS file etc. Commented Jun 14, 2015 at 11:11
  • Why you don't want to use ajax? Commented Jun 14, 2015 at 11:21

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.