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 ?

 

 

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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