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 trialmckbas
14,166 PointsAjax variable not sending?
I've asked this question a few times now, but haven't encountered much luck on the forums or in my internet searches. . Here's to a third & final stab at it. ^^
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 code pulls the div id and matches it in the php file. If they match, the information is pulled from the database. AJAX sends the data but my PHP file says that the variable hasn't been set.
I've tried GET, POST - now I'm not sure what to do.
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"];
echo $_POST['episode_name'];
?>