you should not have a variable ($list) inside single quotes, ' ' without concatinating it
you could have

echo "<a href='#' onclick=select('ALL',"$list")>Select All</a>";

or

echo "<a href='#' onclick=select('ALL',''.$list.'')>Select All</a>";

GJay

06-05-2007, 01:43 PM

the "s at the start and end are the ones that matter, it should be parsed. The code you've given works fine for me, is that exactly what you are using or have you 'simplified' it for the purposes of the question?

Ahri

06-05-2007, 03:07 PM

I don't know what Rafiki is talking about; your use of a PHP variable inside double quotes is perfectly acceptable.

However, I can guess that the reason it only works without a space is because you're not being very kind to browsers; some browsers might work out that you're trying to give 'onclick' the value returned by the function select(), so they might parse until the closing bracket, but honestly if I was writing a browser I wouldn't try to compensate for that sort of laziness, and apparently neither is the browser you're testing in -- it's being charitable enough by assuming that the string is delimited by spaces, as such the value of 'onclick' becomes "select('ALL'," -- so it breaks.

You're supposed to encapsulate string values in quotes in HTML so you're going to have to do it properly;

If you don't like having to escape the quotes then you're better off using PHP as the templating engine it was designed to be;

<a href="#" onclick="select('ALL', '<?=$list;?>')">Select All</a>

rafiki

06-05-2007, 04:06 PM

i wasnt talkin about double quotes i was stating that he had a " '$var' "; and wasnt sure if you should concatinate it, i was giving a lead as to why it might have done it, which may or may not be helpfull, but GJAY corrected it

GJay

06-05-2007, 05:01 PM

My mistake for not actually testing in a browser (just the command-line), Ahri's suggestion makes sense.

Leeoniya

06-06-2007, 12:16 AM

thanks guys. technically, the lack of spaces is really no big deal. but it makes more sense now. i'll go with the escaping option.

i would use this:

<a href="#" onclick="select('ALL', '<?=$list;?>')">Select All</a>

...except the entire page has too much dynamic content to continually go into and out of php mode; most of those echo lines are inside of one-liner foreach() loops, it seems like a silly amount of extra code to justify switching modes that often.

Leon

aedrin

06-06-2007, 01:08 AM

you should not have a variable ($list) inside single quotes, ' ' without concatinating it
you could have

echo "<a href='#' onclick=select('ALL',"$list")>Select All</a>";

or

echo "<a href='#' onclick=select('ALL',''.$list.'')>Select All</a>";

Both of these aren't proper code.

...except the entire page has too much dynamic content to continually go into and out of php mode; most of those echo lines are inside of one-liner foreach() loops, it seems like a silly amount of extra code to justify switching modes that often.

The problem is that with both HTML and JavaScript you're going to have to escape something. Either the JavaScript, or the HTML. I find that often the JavaScript is easier to escape as it is easier to read when your HTML is clean. Using echo prevents PHP from switching in and out of PHP mode which is indeed more intensive.

EDIT: Not to mention that double quotes are the proper quoting method for HTML (at least in XHTML I believe.)