OpenBrWindow error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

OpenBrWindow error

Post by rick.emmet »

Hi Everyone,
I ran some code that I wrote about 11/2 years ago (it ran fine at the time) and now It will not run. I did a little research and found out that the code I used, which I found online and modified, is actually DreamWeaver generated code (no wonder that it stopped working). The code displays a thumbnail image, and if a photo has been uploaded and a user clicks on the thumbnail, a new window opens up and displays a larger image.

I like the structure of this code because it allows for cases where no image was uploaded. The thumbnail image is then a default “No Photo” gif and if a user clicks on the image anyway, a large “No Photo” gif is displayed.

The original code now throws and error, "JavaScript Error: Javascript:MM_openBrWindow is not defined". I made a number of attempts to get rid of the MM_openBrWindow and all failed to even allow the page the code resides on to load. The last attempt allows the page to load and when I place the cursor over the thumbnail, I can see the correct file location in the status bar. But when I click on the image, the new window does not open and I'm redirected to the "Error 404 Object Not Found!" page. I'm not sure why this is happening, it seems as if the browser knows the location of the file.

Here's the code:

Code: Select all

//PHP code, DB Connection & SQL queries here

// fetch the data for this record
$row_rsproducts = mysql_fetch_assoc($rsproducts);

// Original HTML code w/ embeded PHP - which does not run any longer
<td width="132"><?php $image = 'uploads/'.$row_rsproducts['instance_id'].'a.jpg'; if (file_exists($image))	{ $link = 'Javascript:MM_openBrWindow(\''.$image.'\',\'\',\'width=600,height=450\');'; } else { $link = 'Javascript:void(0)'; $image = 'images/no_Photo_Lg.gif'; } ?>
      <a href="<?php echo $link; ?>"><img src="<?php echo $image; ?>" width="132" height="99" border="0"/></a></td>
This throws an error "JavaScript Error: Javascript:MM_openBrWindow is not defined" And here is the new code:

Code: Select all

//PHP code, DB Connection & SQL queries here

// fetch the data for this record
$row_rsproducts = mysql_fetch_assoc($rsproducts);

// New HTML code w/ embeded PHP (no MM_openBrWindow call)
<td width="132"><?php $image = 'uploads/'.$row_rsproducts['instance_id'].'a.jpg'; if (file_exists($image))	{ $link = 'onclick=window.open(\''.$image.'\');'; } else { $link = 'Javascript:void(0)'; $image = 'images/no_Photo_Lg.gif'; } ?>
      <a href="<?php echo $link; ?>"><img src="<?php echo $image; ?>" width="132" height="99" border="0"/></a></td>
Again, I get redirected to the 404 page, and it seems that the browser has the file location. If anyone has a clue why this won't work, I'd appreciate hearing about it. Thank you in advance!
Cheers,
Rick
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OpenBrWindow error

Post by requinix »

Not entirely sure what behavior you're describing but may I offer some advice instead?

Keep the href as an actual URL to the image, then use onclick to open the window (and abort the link-clicking).

Code: Select all

<a href="uploads/123a.jpg" onclick="window.open(this.href, '', 'width=600,height=450'); return false;"><img ... /></a>
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

Hi Requinix,
There are a couple of things I should point out. One, the code is displayed incorrectly the JS call should look like "Javascript:MM_openBrWindow" and not "Javascript&#058;MM_openBrWindow" same is true of the JS void. It should read "Javascript:void(0)" and not "Javascript&#058;void(0)". The full colon is being converted to hexadecimal.

The functionality I need requires that the file addresses for the images are dynamic, I can not hard code them into the app, because there will potentially be thousands of users and tens of thousands of images. The code grabs the "instance_ID" from the database and uses that to locate the correct image file in the uploads folder.

The function must allow for the case where a use doesn't upload a photo - in that case a dummy "No Photo" thumbnail is displayed, and if the user clicks on the "No Photo" thumbnail, a larger version of the image is displayed.

In the case where a use has uploaded a photo, the page displays a thumbnail of the image, and if the user clicks on the thumbnail a new window opens and displays a large version of the photo. The portion of the code that "broke" (it behaves as if it were a deprecated function) is the Javascript:MM_openBrWindow call. I tried several approaches to get that portion of the code working, and the closest I came to getting it right was the line " $link = 'onclick=window.open(\''.$image.'\');'; ". When I place the cursor over the thumbnail, I can see the correct file address for the photo in the task bar, but when I click on the thumbnail, I get the 404 error. It seems that the browser has the proper data to find the image file, but it fails to do so.

I will try more approaches to this, I have written this incorrectly (obviously), but I'm not sure why it is failing. Thanks for your time, and if you have any other ideas, I'd love to hear them!
Cheers,
Rick
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

OK, I feel that I just being messed with now! The hexadecimal characters have been converted back to a full colon!!
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

I should have mentioned this as well, when I get the 404 error the address bar displays this:

http://localhost/siteName/onclick=windo ... 5312a.jpg);

It is not resolving the address. Hope this helps.
Cheers,
Rick
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OpenBrWindow error

Post by requinix »

So what's your code now?
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

Hi Riquinix,
I have been messing with this for about an hour and it seems pretty obvious that the browser could not resolve the address. Trying to call a JS function within PHP using "Javascript:" just doesn't work. The code I just tried is the same as the "new" code above, except for the call to the "window.open", which, by itself, looks like this:

Code: Select all

$link = echo '<script type="text/javascript"> onclick = window.open(\''.$image.'\'); </script>';
But this now throws a an Unexpected T_ECHO error - here's the full function with the code I just tested (but function form instead of on a single line):

Code: Select all

//PHP code, DB Connection & SQL queries here

// fetch the data for this record
$row_rsproducts = mysql_fetch_assoc($rsproducts);

<td width="132">
<?php 
     $image = 'uploads/'.$row_rsproducts['instance_id'].'a.jpg'; 
       if (file_exists($image)) { 
         $link = echo '<script type="text/javascript"> onclick = window.open(\''.$image.'\'); </script>'; 
         } else { 
           $link = 'Javascript:void(0)'; 
           $image = 'images/no_Photo_Lg.gif'; 
} 
?>

<a href="<?php echo $link; ?>"><img src="<?php echo $image; ?>" width="132" height="99" border="0"/></a></td>
I was way too tired yesterday afternoon to think this through. This looks like the correct format to me, but now I have a T_ECHO error. Thanks again for your time! Again the full colon in the "Javascript - full colon - void(0)" call is being converted to hex.
Cheers,
Rick
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OpenBrWindow error

Post by requinix »

That thing I said about keeping the href and the onclick separate... Did you try that?
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

Hi Requinix,
They are separate, the function is run above the href. Also, the code you posted has a hard coded file address. Mine must be dynamic. Am I missing something here?
Cheers,
Rick
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OpenBrWindow error

Post by requinix »

No, they're not separate. You're still trying to shove everything into the href.

Code: Select all

<a href="images/no_Photo_Lg.gif" onclick="window.open(this.href); return false;"><img src="uploads/whatevera.jpg" width="132" height="99" border="0"/></a>
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: OpenBrWindow error

Post by rick.emmet »

Hi Requinix,
I guess I'm not following you. It seems to me (and I should have brought this up earlier) that what you are asking me to use the href tag to make a call to a JavaScript function. Then I would need a JS function in my JS library to handle the two different cases and open a new window with the appropriate image. I am attempting to do this in a PHP function (that makes a JS call) and not in a JavaScript function. Does that make sense to you?

Embedding JS code in PHP should be easy, even without exiting PHP to do so. Using the script tags ('<script type="text/javascript"> and </script>') and placing the JS function in between them should work (so it seems to me). Are you suggesting that this will not work - ever, that I should take a completely different approach? Thanks again for taking the time to reply.
Cheer,
Rick
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OpenBrWindow error

Post by requinix »

rick.emmet wrote:It seems to me (and I should have brought this up earlier) that what you are asking me to use the href tag to make a call to a JavaScript function.
No, I'm specifically saying to not do that. Use the href as an actual URL that could work, but since you want it to open in a new window (and target is deprecated) use a separate onclick handler to (a) open up the new window and (b) stop the link from doing its normal click action of navigating to the href.
rick.emmet wrote:Then I would need a JS function in my JS library to handle the two different cases and open a new window with the appropriate image.
Two different cases? This doesn't change anything: either there's an image to show and open up, or there isn't. The only thing different is the mechanism for showing the popup.
rick.emmet wrote:I am attempting to do this in a PHP function (that makes a JS call) and not in a JavaScript function. Does that make sense to you?
No, because PHP cannot "make a Javascript call". All you can do is output the Javascript and make it so it gets called when you want it to - which is long after PHP finished executing.
Post Reply