Embedding onClick into PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Embedding onClick into PHP

Post by NotOnUrNelly »

Hi All,

I have a number of thumbnail images that are pulled from a database, these are currently displayed down the page.

I have an image place holder named walk.

I want to be able to click any of the thumbnails and the the image in place holder walk will change to the current image.

Here is my full code for the function.

Code: Select all

 
function display_interest_image_details($image_array,$SummitNo)
{
if (!is_array($image_array))
{
echo '<br/>No images listed in this category<br/>';
}
else
{
?>
<div id="Layer2"><img src="images/basket.jpg" name="walk" width="201" height="110" id="walk" /></div>
<?php
echo '<table width = "100%" border = 0 cellspacing =5>';
foreach ($image_array as $Image_Row)
{
$SummitNo = $Image_Row['key_id'];
$Comment = $Image_Row['comment'];
$ImageNo = $Image_Row['id'].".jpg";
$ImagePath = "/wain/images/fells/uploads/Interest/";
echo '<tr><p></p></tr>';
echo '<tr>';
echo '<td width ="100%"><div align="center">';
 
 
 
echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=3';?> onclick="MM_swapImage('walk','','<?php  echo "'.$ImagePath."th_".$ImageNo.'" ?>',1)" 
<?php
//echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=3 >';
echo '</div></td></tr><tr>';
echo '<td width ="100%"><div align="center">';
echo '<b>'.$Comment.'</b>';
echo '</div></td></tr>';
}
echo '</table>';
}
echo '<hr/>';
}
 
 
my problem is on the following line

Code: Select all

 
echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=3';?> onclick="MM_swapImage('walk','','<?php  echo "'.$ImagePath."th_".$ImageNo.'" ?>',1)" 
 
 
I cannnot seem to append the Javascript into my PHP

The following php to display the thumbnails works fine

Code: Select all

 
echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=3 >';
 
 
Can anyone please help me to get this line of code right.

Here is my javascript routine also


function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

Many Thanks
Jamie
thePhpForum
Forum Newbie
Posts: 2
Joined: Thu Feb 28, 2008 11:56 am
Location: Canada

Re: Embedding onClick into PHP

Post by thePhpForum »

Are you getting PHP parse errors on your line
echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=3';?> onclick="MM_swapImage('walk','','<?php echo "'.$ImagePath."th_".$ImageNo.'" ?>',1)"
?


Try something like this:

$url = $ImagePath . 'th_' . $ImageNo;
echo "<img src=\"{$url}\" border=\"3\" onclick=\"MM_swapImage('walk','','{$url}',1)\"";

Code above has not been tested! Might not work right away but you might be able to get at least some basic idea.
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Re: Embedding onClick into PHP

Post by NotOnUrNelly »

Many Thanks for your reply

I seem to have got the page displaying with a little help

Code: Select all

 
function display_interest_image_details($image_array,$SummitNo)
{
if (!is_array($image_array))
{
echo '<br/>No images listed in this category<br/>';
}
else
{
?>
<div id="Layer2"><img src="images/basket.jpg" name="walk" width="201" height="110" id="walk" /></div>
<?php
echo '<table width = "100%" border = 0 cellspacing =5>';
foreach ($image_array as $Image_Row)
{
$SummitNo = $Image_Row['key_id'];
$Comment = $Image_Row['comment'];
$ImageNo = $Image_Row['id'].".jpg";
$ImagePath = "/wain/images/fells/uploads/Interest/";
echo '<tr><p></p></tr>';
echo '<tr>';
echo '<td width ="100%"><div align="center">';
 
 
 
echo '<img src="'.$ImagePath."th_".$ImageNo.'" border=0';?> onclick="MM_swapImage('walk','','<?php  echo $ImagePath."th_".$ImageNo; ?>',1)" >
 
<?php
 
echo '</div></td></tr><tr>';
echo '<td width ="100%"><div align="center">';
echo '<b>'.$Comment.'</b>';
echo '</div></td></tr>';
}
echo '</table>';
}
echo '<hr/>';
}
 
 
The above code displays the following

http://www.runthelakes.co.uk/wain/show_ ... View=Entry.#

The main image does not change when the thumbnail is clicked, Im not sure what the problem is now.

it oculd be the image place holder

Code: Select all

 
 
<div id="Layer2"><img src="images/basket.jpg" name="walk" width="201" height="110" id="walk" /></div>
 
 
Any Idea's Im very new to javascript
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Re: Embedding onClick into PHP

Post by NotOnUrNelly »

Can anyone help with these, Im sure its my Javascript code that could be wrong.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Embedding onClick into PHP

Post by pickle »

What do the arguments to your JS function look like? alert() the arguments to make sure they are what you think they should be.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply