Passing JavaScript variables to PHP
Posted: Sat Jan 22, 2011 9:48 am
Hi, I have a JavaScript code that sets the variable 'arrow' whenever one of the side arrows is pressed like so:
and I have a PHP script that displays a photo gallery. The photos are stored in an array and called using the $keyid variable. I have a next and previous button which increments the keyid so that the next picture is selected and I was hoping to be able to add the same functionality for when the keyboard arrows are pressed using the JavaScript code (I know that part of the page will need to be reloaded but that's fine). Everything works at the moment but I can't work out how to pass the JavaScript variable into the PHP function without using GET or POST in some way. This is my PHP:
Thanks very much for any help!
Code: Select all
<script type="text/javascript">
window.document.onkeyup = getKey; //set event handler
function getKey(e) {
// Store value of key pressed (cross-browser)
var key = e ? e.which : window.event.keyCode;
if (key == 39){
var arrow = "right";
}
else if (key == 37){
var arrow = "left";
}
alert(arrow);
}
</script>
Code: Select all
<?php
function displayPhotos(){
global $columns,$dir,$album, $max_height, $max_width;
generateThumbnails();
$act = 0;
$keyid = (isset($_GET['keyid']) ? $_GET['keyid']:'0');
$Picturecount = (count(glob("" . $dir . "*.jpg")))/2;
$thumb_selected = (isset($_GET['thumb']) ? $_GET['thumb']:'');
$picture_array = glob("$dir*");
if ($thumb_selected !=""){
//Process file name
$dirName = substr($thumb_selected,0,strpos($thumb_selected,basename($thumb_selected)));
$thumbName = basename($thumb_selected);
$thumbFile = $dirName.$thumbName;
$selected = str_replace('_th.jpg','.jpg',$thumbFile);
foreach ($picture_array as $search){
$keyid = array_search($selected,$picture_array);
$large = $picture_array[$keyid];
}
}
else{
if($keyid > (2*$Picturecount-1)){
$keyid = ($keyid - (2*$Picturecount));
}
if($keyid < 0){
$keyid = (2*$Picturecount+$keyid);
}
$large = $picture_array[$keyid];
}
$picturenumber = ($keyid/2)+1;
echo "<table border='0' width='100%'>
<tr align='right'><td align='left' colspan='2'>$picturenumber of $Picturecount</td></tr>
<tr><td><a href='?album=$album&keyid=" . ($keyid-2) . "'><img src='/gallery/icons/arrow-blue-rounded-left.jpg' alt='Previous'></a></td>";
list($width, $height) = getimagesize($large);
if($width > '550'){
imagejpeg(resizeImage($large,$max_width,$max_height),$large,100);
echo "
<td colspan='3' height='300' width='550' align='center'><a href='?album=$album&keyid=" . ($keyid+2) . "'><img src='$large' alt=''></a></td>";
}
else if($height > '300' && $width < '550'){
echo "
<td colspan='3' height='$height' width='550' align='center'><a href='?album=$album&keyid=" . ($keyid+2) . "'><img src='$large' alt=''></a></td>";
}
else if($height > '300' && $width = '550'){
echo "
<td colspan='3' height='$height' width='550' align='center'><a href='?album=$album&keyid=" . ($keyid+2) . "'><img src='$large' alt=''></a></td>";
}
else{
echo "
<td colspan='3' height='300' width='550' align='center'><a href='?album=$album&keyid=" . ($keyid+2) . "'><img src='$large' alt=''></a></td>";
}
echo "<td><a href='?album=$album&keyid=" . ($keyid+2) . "'><img src='/gallery/icons/arrow-blue-rounded-right.jpg' alt='Next'></a></td></tr><tr><td> </td></tr><tr><td> </td></tr></table><table border='0' align='center' width='90%'>";
?>