Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    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 09:09 AM.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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>"

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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

  • #4
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    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

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •