Solved

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

Posted on 2007-03-28
9
679 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Fero45
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18810684
please try like this:
echo "<img src.... ......... onClick='showDress(\'$dress\')'>";
0
 
LVL 6

Author Comment

by:Fero45
ID: 18810756
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
 
LVL 6

Author Comment

by:Fero45
ID: 18810781
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18810909
glad I could help.
as I don't do php/javascript very often, I never know which types of quotes to put around :(
0
 
LVL 6

Author Comment

by:Fero45
ID: 18810939
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
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18811247
You can do this also:

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

Expert Comment

by:Pravin Asar
ID: 18812155
Build the string.

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

0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 18812176
Sorry .. use this one..


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

0
 
LVL 6

Author Comment

by:Fero45
ID: 18853378
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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