2

Ok so I have one page, called albums.php, with the following code:

<?php

    require_once('./config.php');

    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $album = $mysqli->query("SELECT * FROM Albums");

    print('<div id="grid">');
    print('<ul>');
    while ($row = $album->fetch_assoc()) {
        $cover = $row['Album_Cover'];
        $name = $row['Album_Name'];
        $id = $row['Album_ID'];

        print ('<li>');
            print('<form method="POST" action="">');
                print("<input type='image' src=$cover name='image' id='image' class=$id>");
            print("</form>");
            print('<br/>');
            print ("$name");
        print ('</li>');
    }
    print('</ul>');
    print('</div>');

    print('<br/>');
    print('<br/>');
    print('<br/>');
    print('<br/>');

    $mysqli->close();

    ?>

DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME come from config.php. albums.php (again, the code above) is linked to the script button.js, the code for which is below:

$("#image").click(function(e) {
    e.preventDefault();
    var id = $(this).attr('class');
    var dataString = 'id='+id;
    $.ajax({
        type: "POST",
        data: dataString,
        url: "./pictures.php",
        success: function(data) {
            alert(data);
        }
    });
});

My goal is to pass the id of the clicked image to pictures.php using ajax. My code for pictures.php is as follows:

<?php

require_once('./config.php');
require_once('./albums.php');

$id = $_POST['id'];

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$pictures = $mysqli->query("SELECT * FROM Pictures WHERE Album_ID = $id");

print('<div id="grid">');
print('<ul>');
while ($row = $pictures->fetch_assoc()) {
    $picture = $row['Picture'];

    print("<li><img src=$picture class='thumbnail'></li>"); 
}
print('</ul>');
print('</div>');

$mysqli->close();

?>

Do I also have to link the button.js script in pictures.php? Other than that, I can't think of a possible issue with this code. By the way, all three of these files are stored in the same folder on my server, so I believe I am accessing them correctly. Any help would be much appreciated!

12
  • ID's are unique, you cannot have multiple images with ID set to 'image'. It seems you have class and ID mixed up... Commented Mar 18, 2015 at 15:16
  • hv you included jquery? Commented Mar 18, 2015 at 15:19
  • You do not need the javascript if you are not using it and it does not look like you are. Commented Mar 18, 2015 at 15:22
  • codehx, I have included jquery. Mr. White, I switched the class to 'image' and id to $id, but I'm still getting nothing. Also, when I made this switch, all of the styling for the images went away, even though I changed from #image to .image in my stylesheet. Commented Mar 18, 2015 at 15:25
  • RiggsFolly, isn't javascript the only way I would be able to find the id of the clicked image (by id, I mean $id, which has been stored in the class attribute of each image)? Commented Mar 18, 2015 at 15:26

1 Answer 1

2

Just to mention few things:


  1. As mentioned in the comment Ids must be unique but it wont crash your code.

  2. You must have put your js code before the php code that constructs image elements or you have to use jquery's $(document).ready(function(){//your code}); to make sure that you are registering the click event listener for the images.

  3. On your js change:
    var dataString = 'id='+id; to var dataString = id; and
    url: "./pictures.php" to url: "pictures.php"

Sign up to request clarification or add additional context in comments.

3 Comments

I added in $(document).ready and changed the url to pictures.php and that made it start working slightly better. Now when I do console.log(id), the first image prints its id to the console, but the second image does not. Both of them still do not display the images that they contain as they are supposed to. How does var dataString = +id work? When I did this, I got an alert of an unidentified index.
Nevermind, I just got it. I had to change my success function for the html to actually be rewritten. Thanks for the help, I appreciate it.
I would give you an upvote, but I just joined and don't have the reputation for it :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.