calling javascript function with string parameter issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

calling javascript function with string parameter issue

Post by sarris »

Hi there,
simple problem...

i have this php code

Code: Select all

$house_block .= "<a href='#map' onclick='javascript: myfunction(" . $row[coords_X] . " ," . $row[coords_Y] . ")'>Add To Shortlist</a>";
where $row[coords_X] and $row[coords_Y] are doubles

while this works perfectly and the javascript function is called, if i have

Code: Select all

$house_block .= "<a href='#map' onclick='javascript: myfunction(" . $username . " ," . $row[coords_Y] . ")'>Add To Shortlist</a>";
where $username is a string, the function doesnt work...
any ideas plz?
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Post by Xoligy »

You need to quote it in Javascript as well.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

i thought that was the purpose of the " . $variable . "
no? how do i quote it furthermore?
and why would it work with number and not with string?
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Simply use ' in myfunction().

Code: Select all

$house_block .= "<a href='#map' onclick='javascript: myfunction('" . $username . "' ," . $row[coords_Y] . ")'>Add To Shortlist</a>";
String can only be used with quotes. Read this http://www.quirksmode.org/js/strings.html
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

plus when you use the " around a string you do not need to come out of the string to put php variables in

Code: Select all

$house_block .= "<a href='#map' onclick='javascript: myfunction('$username' ,{$row[coords_Y]})'>Add To Shortlist</a>";
This will happily replace the variables in your string, also note the {} around the array variable, you must use these when trying to output an arrays value in a string.

Makes the whole a lot easier to read
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have some nested quote problems guys. ;)
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

You have some nested quote problems guys
As long as you get in the trouble to point out that we have problems, why dont you help out as well and point out the problems?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Have you looked at the code? You can't just put single quotes inside of single quotes just like you can't just put double quotes inside of double quotes.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sarris wrote:As long as you get in the trouble to point out that we have problems, why dont you help out as well and point out the problems?
I did point out the problem. It's a rather simple concept.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

neither what mikeq said works, nor what mentor said works...
any other ideas?
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

It's a rather simple concept.
Apparently it is a simple concept but as you said people have problems with nested quoting. So any help would be appreciated
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read superdezign's reply.
Post Reply