Pass a PHP variable (number or string) a to JavaScript function

Hi
When I pass a numerical variable to a JavaScript function, everything is OK but when I pass it a variable e.g. C123, IE shows a warning window with
Error: 'C123' is undifined:
==================================================
PHP part of the code where  readdir() is used to read all picture files, which measn that the value of  $dress changes:
echo "<img src.... ......... onClick='showDress($dress)'>";
===================================================
JavaScript part of the code:
function showDress(dress) ..
=================================================
SO when $dress = 9547, it works
when $dress = Q123 it does not work even if I pass it as two variables and join them as strings.

Where is the problem?
Thanks
Fero
LVL 6
Fero45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try like this:
echo "<img src.... ......... onClick='showDress(\'$dress\')'>";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fero45Author Commented:
angellll
Thanks for the answer. I tried it, it did not work
IE warning window - Error: Invalid character whowed before all the images displayed
Fero
0
Fero45Author Commented:
Angellll
But I tried \""$dress\" and it worked!!!
First I thought your answer would work because it is the way we pass strings.,
However I except your good idea,
Thanks
Fero
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
glad I could help.
as I don't do php/javascript very often, I never know which types of quotes to put around :(
0
Fero45Author Commented:
abgellll
The thing was that in echo "<img ... I used a couple of single quotes:already
echo "<img src=' ... ' height='123' width='123' ....
Thanks very much :-)
Fero
0
Yury MerezhkovDevelopment Team LeadCommented:
You can do this also:

echo "<img src.... ......... onClick='showDress(".$dress.")'>";
0
Pravin AsarPrincipal Systems EngineerCommented:
Build the string.

echo "<img src.... ......... onClick='showDress(" .   $dress   .  ")'>";

0
Pravin AsarPrincipal Systems EngineerCommented:
Sorry .. use this one..


echo "<img src.... ......... onClick=\"showDress('" . $dress  "')\">";

0
Fero45Author Commented:
angellll
Sorry, Angellll, I thought I gave you the points last week for the answer. I did it today.
Thanx again for the good idea.
Fero
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.