PHP onmouseover更改从数据库调用的图像

PHP onmouseover更改从数据库调用的图像

问题描述:

  echo'<img src="'.$row['filename'].'" onmouseover="this.src='.$row['back_filename'].'" onmouseout="this.src='.$row['filename'].'" />';

I'm calling in 2 images from a database using mySql and php, How come this onmousover doesn't work? ps. I'm calling a path to the image not storing the image in the database itself.

  echo'&lt; img src =“'。$ row ['filename']。'”onmouseover  =“this.src ='。$ row ['back_filename']。'”onmouseout =“this.src ='。$ row ['filename']。'”/&gt;'; 
  code>   pre> 
 
 

我使用mySql和php从数据库调用2个图像,为什么这个onmousover不起作用? \ NPS。 我正在调用图像的路径,而不是将图像存储在数据库本身中。 p> div>

try this

echo'<img src="'.$row['filename'].'" onmouseover="this.src=\''.$row['back_filename'].'\'" onmouseout="this.src=\''.$row['filename'].'\'" />';

You are not providing the needed quotes for the inline javascript, you need single quotes '' around the filename as it is a string, causing whatever the variables hold to be interpreted by javascript as something other than what you expect.

Also use a heredoc to help with preventing errors caused by misquoting and worrying about escaping quotes.

echo <<<END
   <img src="{$row['filename']}" onmouseover="this.src='{$row['back_filename']}'" onmouseout="this.src='{$row['filename']}'" />
END;