call javascript function with php and sending parameters

Posted on 2010-09-06
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 250 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 43

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 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