Use Ajax to send data to external page, and then load said external page
I have a thumbnail gallery populated from a database using php. Each thumbnail is also a link. What I would like is to be able to load an external php page in each case, with the large version of each thumbnail on it, flanked with related images.
The database tables are all set up properly in a relational sense, but I'm not sure about the functionality of being able to send data, in this case imgId
, from the thumbnail gallery page, to the external page, and load the external page at the same time.
I thought it would be possible to do with a form submit, but since I need this functionality on every single thumbnail link, I thought that Ajax would work using jQuery. But alas it doesn't seem to be sending the data when I click on the link.
Hopefully someone can give me some advice. Thanks in advance.
The HTML:
<a target="_blank" href="secondary_imgs.php" class="gallery" value="16">
<img src="new_arrivals_img/thumbnails/boss-skaz1_black_front.jpg">
</a>
The jQuery:
$('.gallery').click(function(){
$.get开发者_C百科("secondary_imgs.php", { imgId: $('.gallery').attr('value') });
});
Use $(this) inside the function to reference the clicked gallery
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') });
});
Try this:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') }, function(data) {
$('body').append(data);
});
});
If that does nothing when you click on it, browse to "/secondary_imgs.php?imgID=xxx" and see if it is returning the data correctly at all.
Ok, I feel kinda stupid, but I figured out that all I needed to do was pass the data through the URL. The PHP code below shows what I mean, and I do thank those who helped me with this problem. My fault for making it faaaar more complicated than needed.
My question is however, is it safe to place data in the href
as I'm showing below? I remember someone mentioning a potent ion problem concerning google spiders and the like.
The PHP:
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<a target='_blank' href='secondary_imgs.php?imgId=".$row['imgId']."'></a>";
}
精彩评论