Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


call javascript function with php and sending parameters

Posted on 2010-09-06
Medium Priority
Last Modified: 2012-06-27
i've an image galerry displaid in php calling a javascript fonction onmousehover
but it isn't called properly

html renders this (for exemple) <img border="0" alt="/img/imagette_10.gif" onmouseover="javascript:newfunction(this,1,en)" src="/img/imagette_10.gif" class="imagette2">
if i put the mouse hover nothing happens but
if with firebug i add 'apostrohers around the two parameters  that means i ve now in my html javascript:newfunction(this,'1','en') it works,
what should i put in my php file to correct my script? sample php code
<?php echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:newfunction(this,'.$resultat["id"].','.$lang.')" border="0" alt="'.$resultat['chemin'].'"  /></a>';

Open in new window

Question by:jerrrrry
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

gplana earned 1000 total points
ID: 33613776
just put this line:
<?php $lang = "'" . $lang . "'"; ?>
before your actual code:
<?php echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:newfunction(this,'.$resultat["id"].','.$lang.')" border="0" alt="'.$resultat['chemin'].'"  /></a>';

This will add apostrophers surrounded the content of $lang into the $lang variable.

Hope it helps.
LVL 44

Expert Comment

by:Chris Stanyon
ID: 33614087
Maybe the PHP shortcut for echoing will help. I often find it easier to echo 'inline', rather than concatenating a string.

If you use the shortcuts, you can echo PHP values anywhere in your HTML with the minimum of fuss.

<p>The value of myVariable is <?=$myVariable?></p>

<a href="#" title="<?=$infos?>"><img class="<?=$sClassName?>" src="<?=$resultat['chemin']?>" onmouseover="javascript:newfunction(this,'<?=$resultat['id']?>','<?=$lang ?>')" border="0" alt="<?=$resultat['chemin']?>" /></a>

Open in new window

LVL 15

Expert Comment

ID: 33616172

<?php echo '<a href="#" title="'.$infos.'" onmouseover="javascript:newfunction(this,'.$resultat["id"].','.$lang.')" >
<img class="'.$sClassName.'" src='.$resultat['chemin'].' border="0" alt="'.$resultat['chemin'].'"  /></a>';

Author Closing Comment

ID: 33616238

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question