0

I have 3 PHP files named as,

  1. index page
  2. books
  3. 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.

5
  • sorry for writing the wrong php file name, just to correct the post, its showbook.php, so that its not confusing to answer. Commented Nov 10, 2012 at 3:10
  • 2
    Please don't use the mysql_* functions as they are in the deprecation process. Use MySQLi or PDO instead. Commented Nov 10, 2012 at 3:13
  • @JasonMcCreary But that is not where i get the error, the error is the popup never opens. All I want to do is to pop the popup on my screen, thanks for the info. Commented Nov 10, 2012 at 3:15
  • 2
    I realize that. I am encouraging you to be a better PHP Developer. Commented Nov 10, 2012 at 3:18
  • @JasonMcCreary Thank you sir, I really appreciate your suggestion. Commented Nov 10, 2012 at 3:20

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

how can i set the inner width and height?
When I click once this code works, when i click second time it does'nt work.
1

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

This code does'nt work, its shows a popup for just a second and then opens a new page.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.