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
sarris
Forum Contributor
Posts: 137 Joined: Mon Dec 04, 2006 2:44 pm
Post
by sarris » Thu Mar 15, 2007 3:59 am
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 » Thu Mar 15, 2007 4:24 am
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 » Thu Mar 15, 2007 4:40 am
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 » Thu Mar 15, 2007 7:17 am
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
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Thu Mar 15, 2007 8:09 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 15, 2007 8:11 am
You have some nested quote problems guys.
sarris
Forum Contributor
Posts: 137 Joined: Mon Dec 04, 2006 2:44 pm
Post
by sarris » Fri Mar 16, 2007 8:38 am
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?
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Mar 16, 2007 8:40 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Mar 16, 2007 8:54 am
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 » Fri Mar 16, 2007 9:12 am
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 » Fri Mar 16, 2007 9:14 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Mar 16, 2007 9:23 am
Read superdezign's reply.