Page 1 of 1
calling javascript function with string parameter issue
Posted: Thu Mar 15, 2007 3:59 am
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?
Posted: Thu Mar 15, 2007 4:24 am
by Xoligy
You need to quote it in Javascript as well.
Posted: Thu Mar 15, 2007 4:40 am
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?
Posted: Thu Mar 15, 2007 7:17 am
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
Posted: Thu Mar 15, 2007 8:09 am
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
Posted: Thu Mar 15, 2007 8:11 am
by feyd
You have some nested quote problems guys.

Posted: Fri Mar 16, 2007 8:38 am
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?
Posted: Fri Mar 16, 2007 8:40 am
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.
Posted: Fri Mar 16, 2007 8:54 am
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.
Posted: Fri Mar 16, 2007 9:12 am
by sarris
neither what mikeq said works, nor what mentor said works...
any other ideas?
Posted: Fri Mar 16, 2007 9:14 am
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
Posted: Fri Mar 16, 2007 9:23 am
by feyd
Read superdezign's reply.