Jump to content

[SOLVED] PHP/Javascript integration issues!!!!


Jago6060

Recommended Posts

I'm trying to use the values from my PHP variables in my javascript functions in order to control the image rollover sizing, heres my code.

 

 

$d1=mysql_result($result,$i,"img_id");
$d2=mysql_result($result,$i,"img_dir");

$img_dim = getimagesize($d2);
$num_img = array(1=>'128','176','320','352','550','640','704','720','768','800','1024');
$width = ($img_dim[0]);
$height = ($img_dim[1]);
$count = 0;

do {$count++;
   $width = ($img_dim[0]/$count);
   $height = ($img_dim[1]/$count);
}
while (($width > 75) && ($height > 75));
echo "
<script language = \"Javascript\" type = \"text/javascript\">
var elemId = addslashes($d1);
var imgHeight = addslashes($height);
var imgWidth = addslashes($width);
function imgEnlarge(){
   document.getElementById(\'elemId\').height += imgHeight
   document.getElementById(\'elemId\').width += imgWidth
}
function imgShrink(){
   document.getElementById(\'elemId\').height -= imgHeight
   document.getElementById(\'elemId\').width -= imgWidth
}
</script>
";

echo "<td><a href=$d2><img src=$d2 id=elemId border=0 width=$width height=$height onmouseover=imgEnlarge() onmouseout=imgShrink()></a></td>";

 

any ideas?

Link to comment
Share on other sites

You have no quote markers around the html attribute values and secondly the javascript is

much safer using a semi-colon to end/delimit each line. Particular relevence is i've found it

almost never operates for HTML element listeners to not have the javascript inside quote

markers and as awful with no semi-colon.

Note: Also put the script "after/following" the HTML in the body, not before when you use the

getElementById DOM syntax system.

Link to comment
Share on other sites

I still can't seem to get it to work.  heres the most recent version I've come up with.

 

$d1=mysql_result($result,$i,"img_id");
$d2=mysql_result($result,$i,"img_dir");

$img_dim = getimagesize($d2);
$width = ($img_dim[0]);
$height = ($img_dim[1]);
$count = 0;

do {$count++;
   $width = ($img_dim[0]/$count);
   $height = ($img_dim[1]/$count);
}
while (($width > 75) && ($height > 75));
echo "
<script language = \"Javascript\" type = \"text/javascript\">
var imgId = ".addslashes($d1).";
function imgEnlarge(){
   document.getElementById(imgId).height += 100;
   document.getElementById(imgId).width += 100;
}
function imgShrink(){
   document.getElementById(imgId).height -= 100;
   document.getElementById(imgId).width -= 100;
}
</script>
";
echo "<td><a href=$d2><center><img src=$d2 id=imgId border=0 width=$width height=$height onmouseover=imgEnlarge() onmouseout=imgShrink()></center></a></td>";

Link to comment
Share on other sites

You only need to write your functions in the javascript once (don't write the function in the while loop)

 

And you mixing javascript and PHP when writing the image tag (id=imgId). This will make all your images have one id "imgId".

 

Also, the while loop shouldn't have a ; after it

 

It should be

while (statment) {
   do something
}

 

You can change your image tag to look like the following

<img src=$d2 border=0 width=$width height=$height onmouseover=imgEnlarge(this) onmouseout=imgShrink(this)>

 

and your functions to this

function imgEnlarge(obj){
   obj.height += 100;
   obj.width += 100;
}
function imgShrink(obj){
   obj.height -= 100;
   obj.width -= 100;
}

Link to comment
Share on other sites

thank you sooooo much! that works just like I need it to.  now my next issue is this.  when the images enlarge they push everything else out of the way.  is there anyway to allow them to just overlap the other images?  and also, is there anyway to preserve the image proportions when doing a rollover enlarge?

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.