I am trying to use a Jquery script called 'fancybox'
I need it so the title will be a link to a selected page, the link needs to include the user and the image name. I can these by PHP $_GET & a Javascript function.
The problem is including them both together as one string if you like. Instead of executing the javascript to return the image name, instead it just displays the code itself.
My question is how do I execute the javascript to include in the link whilst using the PHP still? (it worked before I included the PHP part)
This is what I got so far:
jQuery(document).ready(function(){
var imagename = function( item ) {
return item.href.replace('/images/<?php echo $_GET['user']; ?>/', '');
}
$(".fancybox")
.attr('rel', 'gallery')
.fancybox({
margin : 20,
padding : 10,
openEffect : 'none',
closeEffect : 'none',
nextEffect : 'none',
prevEffect : 'none',
margin : [20, 60, 20, 60], // Increase left/right margin
showCloseButton : true,
showNavArrows : true,
title : '<a href="delete.php?boat=<?php echo $_GET['user'].'&image='; ?>'+imagename+'">Delete this image?</a>'
});
})
This is the link it produces:
http://xxx.xxx.xxx.xxx/delete.php?boat=BoatCV20&image=function%20(%20item%20)%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20item.href.replace('/images/username1/',%20'');%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
Thanks.
imagename
it is a function, so you should call it imagename()
You should call imagename(item)
not pass the reference.
'...?boat=<?php echo $_GET['user']; ?>&image=' + imagename(item) + '">...'
But there is a problem. You expect the item parameter for every image to be there somehow. This is actually not possible this way. You may try to change the title of each image, and then call fancybox by leaving the title option as default:
$(".fancybox img").each(function() {
$(this).attr('title', '...?boat=' + user + '&image=' + imagename(this) + '">...'
});
Also I would recommend to URLencode the image path you put in the uri:
'...' + encodeURIComponent(imagename()) + '...'
and using the PHP part in one place like this:
var user = "<?php echo $_GET['user']; ?>";
Then using the user
JavaScript variable later. It is much more readable.
I should do it only in javascript since you are not doing any server side calculation. You can use a plugin like this https://github.com/allmarkedup/purl to take the parameters you want from url or you can search over stackoverflow for many other lighter functions
What is item? If it's a global var defined before jQuery(document).ready, you can do:
var imagename = item.href.replace('/images/<?php echo $_GET['user']; ?>/', '');
Thanks for all the help, finally got there in the end with the follwing code:
jQuery(document).ready(function(){
$(".fancybox")
.attr('rel', 'gallery')
.fancybox({
margin : 20,
padding : 10,
openEffect : 'none',
closeEffect : 'none',
nextEffect : 'none',
prevEffect : 'none',
margin : [20, 60, 20, 60], // Increase left/right margin
showCloseButton : true,
showNavArrows : true,
title : function( item ) {
var imagename = item.href.replace('/images/<?php echo $_GET['user']; ?>/', '');
return '<a href="delete.php?boat=<?php echo $_GET['user'].'&image='; ?>'+imagename+'">Delete this image?</a>'
}
});
})