Solved

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

Posted on 2007-03-28
9
675 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now