Solved

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

Posted on 2007-03-28
9
670 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
Comment Utility
please try like this:
echo "<img src.... ......... onClick='showDress(\'$dress\')'>";
0
 
LVL 6

Author Comment

by:Fero45
Comment Utility
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
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
glad I could help.
as I don't do php/javascript very often, I never know which types of quotes to put around :(
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Author Comment

by:Fero45
Comment Utility
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
Comment Utility
You can do this also:

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

Expert Comment

by:Pravin Asar
Comment Utility
Build the string.

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

0
 
LVL 28

Expert Comment

by:Pravin Asar
Comment Utility
Sorry .. use this one..


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

0
 
LVL 6

Author Comment

by:Fero45
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now