I have 3 PHP files named as,
- index page
- books
- user
Index Page
<link href="colorbox.css" type="text/css" rel="stylesheet" media="all" />
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery.colorbox.js"></script>
<script type="text/javascript">
function bookRetr(str)
{
if (str=="") {
document.getElementById("more-info").innerHTML="";
return;
}
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("more-info").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","showbook.php?id="+str,true);
xmlhttp.send();
}
</script>
<script>
$(document).ready(function() {
//Examples of how to assign the ColorBox event to elements
$(".popup").colorbox({iframe:true, innerWidth:750, innerHeight:520});
});
</script>
<div id='$bookid' onClick="bookRetr(this.id)></div>
<div id='more-info'></div>
bookshow.php
$bookid = $_GET['id'];
$query = mysql_query("SELECT * FROM bookdatabase WHERE ID='{$bookid}'");
$fetch = mysql_fetch_array($query);
$user = $fetch['userID'];
echo "<a href='showuser?id=$user' class='popup'>My name is X</a>";
The book show echo part is shown in my index page when i click on the div, but when click on My name is X, it open a new page but its actually supposed to open a popup. I got the popup named as colorbox plugin.
I'm unable to figure out where exactly i'm going wrong that the popup never opens.
3 Answers 3
Instead of:
$(".popup").colorbox({iframe:true, innerWidth:750, innerHeight:520});
Try:
$('.popup').live('click', function() {
$.colorbox({href:$(this).attr('href'), open:true, iframe:true, innerWidth:750, innerHeight:520});
return false;
});
OR if it won't work:
$("body").on("click", ".popup", function() {
$.fn.colorbox({href:$(this).attr('href'), open:true, iframe:true, innerWidth:750, innerHeight:520});
return false;
});
Hope it helps a little
2 Comments
your problem is that you are dynamically generating the link, You will have to use jquery live to make it work. Here is the code
$(function(){
//Examples of how to assign the ColorBox event to elements
$(".popup").live("click",function(){
$.colorbox({iframe:true, innerWidth:750, innerHeight:520});
return false;
});
});
Dins
1 Comment
Because you need to hijack the natural execution of clicking a href link...you need to add return false , which will essentially tell the browser to not treat your href like a link
If you change your popup function like this it should work, course readd your colorbox opts after the href opt by comma separation like so
$('.popup').click(function(){
$.colorbox({href:$(this).attr('href'), iframe:true, innerWidth:750, innerHeight:520});
return false;
});
Added your opts and code cleanup
mysql_*functions as they are in the deprecation process. Use MySQLi or PDO instead.