Jump to content

Syntax help - PHP code inside JAVASCRIPT function..


krembo99

Recommended Posts

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 ?

 

 

 

 

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 ...

 

  • 4 weeks later...

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>';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.