...

View Full Version : can anyone reproduce this?...WIERD.



Leeoniya
06-05-2007, 08:06 AM
I think my PHP engine is broken. The result of the first version in place of where 'contacts' should appear is wrong. i get the following: " contacts=""


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

with the space removed after 'ALL', everything works fine:

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

anyone?
Leon

rafiki
06-05-2007, 12:22 PM
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, 12: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, 02: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;


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

That should parse just fine on any browser.

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, 03: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, 04:01 PM
My mistake for not actually testing in a browser (just the command-line), Ahri's suggestion makes sense.

Leeoniya
06-05-2007, 11:16 PM
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, 12: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.

Correct. It is silly.

It is much better to do it like this:



$result = array(/* content */);

foreach ($result as $list) {
echo '<a href="#" onclick="select(\'ALL\', \'' . $list . '\'); return false;">';
}


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.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum