I'm trying to get some information from my albums class. I think my issue is in the syntax of my AJAX call. Let me break this down for you step by step. Here's the method:
Album.php
...
public function getTracks ($title) {
$db = Dbclass::getDB();
$query = "SELECT *
FROM upcoming_albums_tracks
WHERE albums_title = :title";
$statement = $db->prepare($query);
$statement->bindParam(':title', $title, PDO::PARAM_STR, 50);
$statement->execute();
$tracks = $statement->fetchAll();
return $tracks;
}
This method is working fine, by the way. Now here's my php file that calls this method:
GetTracks.php
<?php
require_once '../../models/database.php';
require_once 'Album.php';
$tracks = new Album;
$tracks->getTracks($_POST['albumTitle']);
return $tracks;
And finally, the AJAX call
upcoming_albums_ajax.js
...
$(document).ready(function() {
//Get track info with Ajax
$(".btn-tracks").click(function (e) {
// stop form submission first
e.preventDefault();
// get album title
var albumTitle = $(this).val();
console.log(albumTitle) //This gives me the value I'm looking for.
// get tracks from php
$.ajax({
url : '../../controllers/admin/GetTracks.php',
//I think the issue is in how I'm formatting the data.
data: {title: albumTitle},
type : 'POST',
success : function (d) {
alert(d);
},
error : errorHandler
});
});
});
My alert just pops up telling me that I have an undefined index: albumTitle.
By the way, this is my button:
<button type='submit' class='btn-tracks' value='" . $album['albums_title'] . "'>Show Tracks</button>