2

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>
Jordan Carter
  • 1,276
  • 3
  • 19
  • 43

1 Answers1

1

In GetTracks.php you expect to receive $_POST['albumTitle']

$tracks->getTracks($_POST['albumTitle']);

But you pass in $_POST['title'] from javascript

data: {title: albumTitle},

Change either (so they're equal) and you should be fine

JimL
  • 2,501
  • 1
  • 19
  • 19
  • I did that, but just get nothing at all in the alert. At least the undefined index error went away, though. – Jordan Carter Apr 10 '16 at 19:29
  • I think I solved the issue. In my Album.php file I simply returned $tracks. I suppose when I alert this, it isn't able to alert the array. I tried var_dumping it and it showed all the information just fine. Now I suppose all that's left is to use jQuery to populate my page with the information! Thanks for your help. – Jordan Carter Apr 10 '16 at 19:33