Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-05-2007, 08:06 AM   PM User | #1
Leeoniya
Regular Coder

 
Join Date: Apr 2006
Location: Northbrook, IL
Posts: 388
Thanks: 8
Thanked 6 Times in 6 Posts
Leeoniya is on a distinguished road
Question can anyone reproduce this?...WIERD.

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=""

PHP Code:
$list "contacts";
echo 
"<a href='#' onclick=select('ALL', '$list')>Select All</a>"
with the space removed after 'ALL', everything works fine:
PHP Code:
$list "contacts";
echo 
"<a href='#' onclick=select('ALL','$list')>Select All</a>"
anyone?
Leon

Last edited by Leeoniya; 06-05-2007 at 08:09 AM..
Leeoniya is offline   Reply With Quote
Old 06-05-2007, 12:22 PM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
you should not have a variable ($list) inside single quotes, ' ' without concatinating it
you could have
PHP Code:
echo "<a href='#' onclick=select('ALL',"$list")>Select All</a>"
or
PHP Code:
echo "<a href='#' onclick=select('ALL',''.$list.'')>Select All</a>"
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 06-05-2007, 12:43 PM   PM User | #3
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
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?
__________________
My thoughts on some things: http://codemeetsmusic.com
And my scrapbook of cool things: http://gjones.tumblr.com
GJay is offline   Reply With Quote
Old 06-05-2007, 02:07 PM   PM User | #4
Ahri
New Coder

 
Join Date: May 2007
Location: Manchester, UK
Posts: 72
Thanks: 0
Thanked 2 Times in 2 Posts
Ahri is on a distinguished road
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;

PHP Code:
$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;

Code:
<a href="#" onclick="select('ALL', '<?=$list;?>')">Select All</a>
__________________
Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini
Ahri is offline   Reply With Quote
Old 06-05-2007, 03:06 PM   PM User | #5
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
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
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 06-05-2007, 04:01 PM   PM User | #6
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
My mistake for not actually testing in a browser (just the command-line), Ahri's suggestion makes sense.
__________________
My thoughts on some things: http://codemeetsmusic.com
And my scrapbook of cool things: http://gjones.tumblr.com
GJay is offline   Reply With Quote
Old 06-05-2007, 11:16 PM   PM User | #7
Leeoniya
Regular Coder

 
Join Date: Apr 2006
Location: Northbrook, IL
Posts: 388
Thanks: 8
Thanked 6 Times in 6 Posts
Leeoniya is on a distinguished road
Smile

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:
PHP Code:
<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
Leeoniya is offline   Reply With Quote
Old 06-06-2007, 12:08 AM   PM User | #8
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
Quote:
Originally Posted by rafiki View Post
you should not have a variable ($list) inside single quotes, ' ' without concatinating it
you could have
PHP Code:
echo "<a href='#' onclick=select('ALL',"$list")>Select All</a>"
or
PHP Code:
echo "<a href='#' onclick=select('ALL',''.$list.'')>Select All</a>"
Both of these aren't proper code.

Quote:
...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:

PHP Code:
$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.)
aedrin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:49 PM.


Advertisement
Log in to turn off these ads.