krembo99 Posted November 23, 2009 Share Posted November 23, 2009 I have a problem knowing the right syntax to execute PHP inside Javascript ... In one of the templates I made for a WP based I have a mouseover "tooltip" written in Javascript. it is triggered within a <SPAN> tag with calss "tip", like so : <span class="tip" onmouseover="tooltip('text of tooltip');" onmouseout="exit();"> <?php postimage(thumbnail) ?> </span> the <?php postimage(thumbnail) ?> is a function I wrote, that outputs a thumbnail path of an Image... Now, this is working great, when I do mouseover the image, the tooltip text is displayed. But , when I try to replace the tooltip text, with a PHP function like so : <span class="tip" onmouseover="tooltip('<?php postimagehover(medium);?>');" onmouseout="exit();"> <?php postimage(thumbnail) ?> </span> , the whole code is messed up ... (This is the postimagehover function I wrote, if it can help ... <?php function postimagehover($size=medium) { if ( $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image',))) { foreach( $images as $image ) { $attachmenturl=wp_get_attachment_url($image->ID); $attachmentimage=wp_get_attachment_image( $image->ID, $size ); echo '<div><img src="'.$attachmenturl.'" /></div>'; } } else { echo "no images found"; } } ?> ) I am sure it is some syntax error, but I just do not know the right way of putting that PHP function INSIDE the Javascript . I have tried double brackets, slashes, etc but to no avail. Trial and error will not work here :-) can anyone help me by indicating the right way to do so ? Quote Link to comment Share on other sites More sharing options...
trq Posted November 23, 2009 Share Posted November 23, 2009 Have you defined the constant medium anywhere? Quote Link to comment Share on other sites More sharing options...
krembo99 Posted November 23, 2009 Author Share Posted November 23, 2009 yes, it is defined within the WP system, and in fact, outputs the RIGHT url ..... The PHP part , is not the problem IMHO.. the problem is that the <img> closing tag ">" messes up the code... When I put a normal text, all working OK... when I put <img src="some path to image.jpg" alt="something" /> the "/>" part, that actually ends the IMG tag, is parsed wrongly, which results in outputting everything that comes after it , E.G. );" onmouseout="exit();"> like plain text which display over the image, and not seeing it like a JAVASCRIPT anymore, but like text .. in other words, it breaks the code ... I actually see the text ");" onmouseout="exit();">" displayed on the website ... Quote Link to comment Share on other sites More sharing options...
krembo99 Posted December 20, 2009 Author Share Posted December 20, 2009 IF someone is having the same problem : I have resolved it by just dropping the double quotes ("). The ING SRC still see the path even with no quotes.. from echo '<div><img src="'.$attachmenturl.'" /></div>'; to echo '<div><img src='.$attachmenturl.' /></div>'; Quote Link to comment Share on other sites More sharing options...
trq Posted December 20, 2009 Share Posted December 20, 2009 But now your markup is invalid. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.