Forum Moderators: coopster

Message Too Old, No Replies

JavaScript and PHP

Why won't this work

         

aaronjf

9:46 pm on Feb 2, 2006 (gmt 0)

10+ Year Member



I am trying to set some existing code to give a popup window of a larger product image. The Javascript is printing, but does not respond in the browser. Using OnClick MM_openBrWindow. Can anyone see what the problem is. Drving me nuts.

<?php
if(! (trim($rs["pImage"])=="" ¦¦ is_null($rs["pImage"]) ¦¦ trim($rs["pImage"])=="prodimages/")){?>
<img class="prodimage" src="<?php print $rs["pImage"]?>" border="0" alt="<?php print str_replace('"','&nbsp;',$rs[getlangid("pName",1)]) ?>" onclick="MM_openBrWindow('<?php print $rs["pLargeImage"]?>','','resizable=yes,width=210,height=410')" /> <?php
}elseif(! (trim($rs["pLargeImage"])=="" ¦¦ is_null($rs["pLargeImage"]) ¦¦ trim($rs["pLargeImage"])=="prodimages/")) {?>
<img class="prodimage" src="<?php print $rs["pLargeImage"]?>" border="0" alt="<?php print str_replace('"','&nbsp;',$rs[getlangid("pName",1)])?>" /> <?php
}else
print "&nbsp;";?>

StupidScript

12:28 am on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry but it's hard to understand what is happening.

This code shows as you expect in the page source after it has loaded, but when you click the image the Javascript is not executing?

Not too surprising, really, as the onClick event is not associated with the IMG tag, except spotty support in some MS browsers (4.x comes to mind).

That is to say that it MAY work in some circumstances, but it's not 'real'.

Try using an anchor tag around the images and including your Javascript call as:

<a href="javascript:void MM_openBrWindow('<?php echo $rs["pLargeImage"]?>','','resizable=yes,width=210,height=410')">

instead of using it as an IMG tag attribute.

To tidy up, you'll want to add a closing brace to your final

else
condition, too.

Lastly, note how I used 'echo' instead of 'print', because the PHP print function uses more server resources than echo does, and you want to keep your code as performance-conscious as you can before things get really crazy. ;)

aaronjf

3:46 am on Feb 3, 2006 (gmt 0)

10+ Year Member



Cool thank you. I will give it a try.

Just for my own edification. How does print use more than echo?

aaronjf

3:59 am on Feb 3, 2006 (gmt 0)

10+ Year Member



Strange. Did not work. The other strange thing is that the origanal script used as an image atribute was generated by Dreamweaver MX. Been using it for years in static HTML pages and worked fine.

aaronjf

4:23 am on Feb 3, 2006 (gmt 0)

10+ Year Member



I added the script language tag and it started working. How does this look?

<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<a href="javascript:void MM_openBrWindow('<?php echo $rs["pLargeImage"]?>','','resizable=yes,width=210,height=410')"><img src="<?php print $rs["pImage"]?>" alt="<?php print str_replace('"','&nbsp;',$rs[getlangid("pName",1)]) ?>" border="0" class="prodimage" /></a>

StupidScript

4:33 am on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First answer attempt ... (feel free to jump in) ...

How does print use more than echo?

The PHP

print()
'function' (<- that classification being the key) prepares the presentation for the 'drawing' procedure utilizing the video subsystem and its generalized rendering instructions.

The PHP

echo
'instruction' (<- note the single-quote pseudo-class delimiters) tells the shell to send output to 'stdout', which, in the case of the preparation via PHP of a web page for distribution to a web browsing client, results in the direct in-out data flow that delivers a nearly bit-for-bit representation of the intended result.

To sum up;

print()
is a little more resource-intensive but is still useful while
echo
is faster and is more useful in more situations.

Second answer attempt ... (likewise) ...

Sounds like a Javascript function issue to me. Maybe the data isn't being passed properly, or interpreted properly, or something. Are you getting any kind of error message? What browser are you using for these tests?

<edit>That looks fine. Don't forget to make your tag code as 'accessible' as possible.

See the JS

void
? That tells the interpreter to NOT send anything to the browser's display interpreter. Kinda like the PHP
echo
instruction; do the thing but, in this case, DON'T inform the video ... let the new window instance do that ... no need for two requests. Performance. ;)</edit>

<edit2>Hmmm... adding the 'language' attribute made it work? Really? Wow. Sounds like a browser bug, or are you using Netscape 4.x? (hehe, just kidding :) Anyway, nice to hear a successful result. Happy motoring!</edit2>

aaronjf

4:40 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Cool. Thanks for the education. I really appreciate it.

On a Mac, I am using FireFox 1.0.7 and Safari 2.0.3.