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

Sending AJAX/PHP Variables

I've searched the web for an answer to this several times over but can't seem to find a working solution.

I've excluded a lot of the code for readability & sake of minimalism. The project I'm working on is supposed to open a modal window & load in content from the database.

My problem is that the variable isn't being set in PHP. Literally everything else is working with the exception of this variable pulling through. My code pulls the div id name and matches it in the php file. If they match, the information is pulled from the database. As of right now, my variable is empty... so no match. If I hardcode the variable, it works perfectly - but that isn't helpful in this situation.

I've tried GET, POST, I've tried assigning the value to a cookie. Literally nothing works.

Any ideas would be greatly appreciated.

<div id="despair" class="cartoon" onclick="openEp(this.id)">
<p>content here</p>
</div>

<div id="friday" class="cartoon" onclick="openEp(this.id)">
<p>different content here</p>
</div>
function openEp(clikId) {
episode_name = clikId;

$.ajax({
     type: 'POST',
     url: 'cartoons.php',
     data: ({episode_name: episode_name}),
     success: function(data) {
      console.log('sent ' + episode_name);
     }
    });
}

and for my cartoons.php file

<?php include 'connect.php';

$conn = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

$episode_name = $_POST["episode_name"];

if (isset($_POST["episode_name"]) && !empty($_POST["episode_name"])) {
echo $_POST['episode_name'];
} else {
 echo "not assigned";
}


?>

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Okay, it's been forever since I've used php and ajax, and I can't easily replicate this test on my machine, but I'll point out a few things to check, and how I'd troubleshoot from here.

  1. Make sure you have jquery included properly on the page
  2. Make sure your ajax url is correct. Is the ajax call actually finding cartoons.php?
  3. Confirm that episode_name is actually getting a value to pass along
  4. Confirm connect.php is being found
  5. Confirm your database connection credentials are correct

A couple other notes. success is a callback that is called if the request is successful. You're passing in data to the anom function, which is good, but then you're calling console.log() on the episode_name which I don't think will be defined. data would hold the value of what eventually would be returned by the server.

Let me know how those go