#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0

    Echo cant put quotes properly


    Code:
    echo "<th id='th$i' ><a href='' value='aa' onclick='window.open('windowing.php?q=' + document.getElementById('$myImage').value);'><input type='image' id='$myImage' value='10.255.255.255' src='img/ok.png' width='25' height='25'></a></th >";
    Already tired of trying different combinations of ' and " can anyone show the right way to do it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,967
    Rep Power
    374
    are you having any problems or just wanting the correct way?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by paulh1983
    are you having any problems or just wanting the correct way?
    Having problem understanding how to fix situations like this
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Originally Posted by Stobor
    can anyone show the right way to do it.
    Yes: Don't write spaghetti code.

    You have JavaScript nested in HTML nested in PHP. It's only natural that you end up with a big syntax mess of different languages clashing with each other.

    Isn't this problem a pretty clear warning sign? It tells you to untangle your languages: Use external JavaScript files instead of the old inline attributes. Consider using a dedicated template engine like Twig or Smarty to get rid of the PHPHTML mixture.

    Comments on this post

    • ManiacDan disagrees : Your posts aren't worth reading, and don't forget to use PDO.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Blackburn, Lancashire.
    Posts
    8
    Rep Power
    0
    Hi,

    If I understand correctly, you can output a quote in a string by escaping it with a backslash.

    For example:-
    PHP Code:
    echo '<p>The word isn\'t has an apostrophe that needs escaping.</p>'
    Hope that's what you mean.

    petemaxi.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Without you actually describing the problem it's difficult to figure out what you're having trouble with. Your problem is with this bit, I assume:
    Code:
    onclick='window.open('windowing.php?q=' + document
    You OPEN a single-quoted string at the HTML level right after the equals sign, and then CLOSE it again right after the left parentheses. There are 3 quotes in that snippet, but the first one signifies the beginning of an entirely different LANGUAGE (Java Script).

    The actual solution to this is to stop mixing 3 languages on the same line. Passing dynamic JS around everywhere isn't correct. Your window-opening function should be a separate, named function in a separately-sourced JavaScript file. Which would make your more-corrected string:
    PHP Code:
    echo "<th id='th{$i}' ><a href='' value='aa' onclick=\"openWin('{$myImage}');\"><input type='image' id='{$myImage}' value='10.255.255.255' src='img/ok.png' width='25' height='25'></a></th >"
    I did 3 things here:
    1) Changed the flat JS to a function call
    2) Changed the quotes around your "onclick" to escaped double-quotes
    3) Put brackets around your PHP variables as is proper under complex string syntax like you're using.

    Generally, top-level PHP projects don't mix and match code like this. Templating languages or separate template files are used strictly for HTML rendering so you're not doing math, printing HTML, and printing JavaScript on the same messy, confusing line.

    Comments on this post

    • Jacques1 disagrees : If you stopped ignoring my posts, you wouldn't have to repeat everything.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Thanks for the support Dan managed to fix it. Care to explain why it is better to call a js function to open a php ? it seems easier to call it from html directly less code easier to read .
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    As far as easier and whether it's better or not, my opinion would be more a How often will you do such? If this is really it, just include it as you have, but if it will commonly be used, make it a function that can easily be called all over.

    As per a solution, sorry if this is horrible, but simply adjusting your 'onlick' attribute to use " instead of ' would work. Make sure to escape them so they don't step back into PHP.

    And I'm not sure if one way is better or not, but I always step in/out when referencing a variable....
    PHP Code:
    echo 'Yesterday, when ' $name ' went to the store...'
    He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Originally Posted by Stobor
    Care to explain why it is better to call a js function to open a php ? it seems easier to call it from html directly less code easier to read .
    In fact, you shouldn't have any JavaScript flying around in your HTML -- or as little as possible. That's what we're trying to tell you. Everything else is just a workaround.

    I'm pretty sure that the JavaScript attributes will sooner or later die out, just like the style attributes (align, color etc.) have died out. Why? Because HTML describes the content and structure of a document. It doesn't define its layout. That's what CSS is for. And it doesn't define its functionalities. That's what JavaScript is for.

    On modern webpages, you have all style definitions in external CSS files and all functionalities in external JavaScript files. No color="red", no onclick="alert(42)". Every language only does what it's made for.

    You may think this is just a theoretical discussion, but it's not. If the webpage becomes bigger and you keep adding new JavaScript attributes, you'll quickly end up with an unreadable mess. I cannot even read the code snippet you posted. I understand what it does, but I can't see where things start and where they end. That's a warning sign.

    And if you ever have to work on a big HTML document written by somebody else, you'll hate them for using inline JavaScript.

    Modern JavaScript frameworks like jQuery make it very easy to select HTML elements from external JavaScript files. Use them!
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo