0

I'm trying to create a liking function for my image site so users can like or dislike images... They start off on the homepage where they can see a thumbnail of the image. Once they click a thumbnail, they're brought to viewimage.php to view the full size. It's there that they can like.

The issue is, the associated $imgid is not being updated, so once you load viewimage.php, I'm stuck with that $imgid, and consequently the associated total likes.

homepage.php: contains small thumbnails of each full-sized image. As you can see, if they click on a thumbnail, one of the parameters sent to viewimage.php is $row['id'], which I get with a $_GET.

while ($row = $catResult->fetch_assoc()) {
   ...
   echo '<a href=".?action=viewimage&site='.$site. '&id=' . $row['id'] .'"><img src ... /></a>'
   ...
}

viewimage.php After the click on a thumbnail, they're show the full size image on viewimage.php. Users can navigate images by pressing left and right arrow... This would modify javascript variable "imgIndex", which is set to the server side $imgid.

$imgid = $_GET['id']; 

include './scripts/keyinput.php'; 

$newID = $_POST['_postID'];
echo $newID;

keyinput.php Javascript that's included in viewimage.php. In this code snippet, when the user presses the right arrow, the $post should alter the server side $imgid, but it's not.

<script type="text/javascript">
  var imgArray = [<?php echo implode(',', getImages($site)) ?>];
  var imgIndex = <?php echo $imgid ?>;

  $(document).ready(function() {

     $(document).keydown(function (e) {
         if (key == rightarrow) { 
             imgIndex++;
             $.post('./templates/viewimage.php', { _postID : imgIndex });
         }
         ...
    });
</script>


<?php
function  getImages($siteParam) {
include 'dbconnect.php';
if ($siteParam == 'artwork') { 
    $table = "artwork"; 
}       
else { 
    $table = "comics"; 
}   

$catResult = $mysqli->query("SELECT id, title, path, thumb, views, catidFK FROM $table");   
$img = array();
while($row = $catResult->fetch_assoc()) 
{
    $img[] = "'" . $row['path'] . "'";
}
return $img;
}
?>

It gives me an error: Undefined index: _postID in C:\wamp\www\HTwS\templates\viewimage.php on line 16

Why can't the $.post function on keyinput.php send the updated imgid back to viewimage.php?

Thank you!

user3871
  • 12,432
  • 33
  • 128
  • 268
  • 1
    add console.log(imgIndex); right after `imgIndex++;` and let us know what it is set to. – Matt Feb 18 '13 at 22:55
  • @mkaatman how do I view the value? where do I go? – user3871 Feb 18 '13 at 22:57
  • 1
    If you are using Chrome or Firefox hit Ctrl+Shift+J. That will bring up the javascript console. P.S. I don't see where you've started imgIndex at 0 so it likely can't increment. – Matt Feb 18 '13 at 22:58
  • @mkaatman each time I press right arrow, it increments by 1, which is correct – user3871 Feb 18 '13 at 23:02
  • In Chrome, Open debugger using `Ctrl + Shift + J` -> under `network tab` you can monitor the `post` request, check the `Form Data` under `Headers` to see the value sent to server – Arun P Johny Feb 18 '13 at 23:03
  • @mkaatman I set imgIndex = ; So it starts off at the correct ID – user3871 Feb 18 '13 at 23:03
  • Is there more code in viewimage.php? Where is it fetching the image? Or maybe that's happening in keyinput.php? – Matt Feb 18 '13 at 23:07
  • @mkaatman updated above – user3871 Feb 18 '13 at 23:10
  • can you do a print_r$(_POST); in your viewimage.php at the very top of the file to see if the variable is even there? It may be the extra whitespace you have in this line of code { _postID : imgIndex }); I would get rid of the space between _postID and the colon, and get rid of the space before the underscore in _postID. Also...as an extra sanity check, you might want to change imgIndex to imgIndex + '' to make sure it's being passed as a string. – rnirnber Feb 18 '13 at 23:54
  • @Growler Which one is line 16? – Matt Feb 19 '13 at 01:38
  • @mkaatman line 16 is referring to '$newID = $_REQUEST['_postID'];' – user3871 Feb 19 '13 at 02:14

0 Answers0