Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Travis Stewart
Travis Stewart
15,188 Points

Using AJAX to dynamically load information from a PHP file

Hi Treehouse Family. I was hoping you could help me with a project I am working on.

I want to use ajax requests to load php files that are based on db info. So a user clicks on the link to a category of things, ajax/jquery loads inner html of #container with resulting info from the stuff.php file. Like a single page app.

Although I can see my html code from stuff.php coming through when I call it, I keep getting error messages from my foreach loop ("$stuff not declared" in my current configuration). It appears my variables in my index.php file will not communicate with the rule loaded by stuff.php when it is called. I've tried moving around my require statements and variables to different parts of the files (including into stuff.php) with no success. What am I missing here? Is this even possible? I can't find anyone else who has tried to do this. Thanks

This is my index code

<?php 
     require_once("models.php"); 
     $stuff = functional_sql_statement(); 
?>
<div id="container">
    <a href="#" onclick="sendAJAX">Load List of Things</a>
</div>

This is my jquery code

function sendAJAX(){
    $('#container').load('stuff.php');
    console.log('sendAJAX'); 
}; 

This is the list view from stuff.php I am trying to load

<?php       
    foreach($stuff as $thing){
        echo '<li><a href="./things?id='. $thing["id"] .'">' . $thing["name"] . '</a></li>'; 
    }
?>

1 Answer

Ron McCranie
Ron McCranie
7,837 Points

When you make a request to a file using the jQuery load() method, it sends data just like any regular request through your browser. It has no knowledge of any of your existing PHP variables unless you pass them along with your ajax request as data. See jQuery's documentation

Your data, or $stuff should be in a json object like this...

function sendAJAX(){
    $('#container').load(
    "stuff.php",  // url
    { // json object of data to pass to the page
        stuff: "all your stuff goes in here...",
        moreStuff:  "even more stuff"
    });
    console.log('sendAJAX'); 
}; 

Then you have to be looking for that stuff on the PHP file.

Travis Stewart
Travis Stewart
15,188 Points

Ah! I was totally stuck. I want my db connections and queries in php. So I was only thinking in php. It looks like I can use json_encode() to pass the results to javascript and build my list view from there. I'm going to try it out. I'll let you know how it goes. Thanks!