Solved

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

Posted on 2007-03-28
9
685 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
[X]
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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 143

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 143

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 29

Expert Comment

by:Pravin Asar
ID: 18812155
Build the string.

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

0
 
LVL 29

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

627 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