-2

We all know that a quotation mark must have it's partner. But how will I do it if I got 3 consecutive elements that wil use up a quotation mark (PHP echo, HTML and CSS class, Javascript function)? Something like this

echo '
    <tr class="table-body" onclick="showMenu('edit-user-window')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 

The echo's quotation mark breaks at the start of my onclick function. How do I do it?

Gibs
  • 109
  • 1
  • 4
  • 12

6 Answers6

4

You can simply escape it:

echo ' <tr class="table-body" onclick="showMenu(\'edit-user-window\')">.....';

Notes

The error you are receiving comes because you effectively "break" the string at those points. It is bad concatenation. And by using the \ before those commas ('), you're telling the php interpreter to ignore the character that follows that slash (\) which, in this case is the comma (').

Darren
  • 13,050
  • 4
  • 41
  • 79
0

Skip it using the slash

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
';
0

You can use \ to escape a character, so your example would look like:

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 
Yago Arroyo
  • 38
  • 1
  • 9
0

Use backslash like this...

onclick="showMenu(\'edit-user-window\')"

Backslashes are used in PHP to escape special characters within quotes.

jned29
  • 477
  • 12
  • 50
0

Use either a backward slash \ before ' -> \' (also works if you work with strings encapsulated in " -> \")

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 

or use heredoc syntax

echo <<<EOT
    <tr class="table-body" onclick="showMenu('edit-user-window')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
EOT; 
Ke Vin
  • 2,004
  • 1
  • 18
  • 28
0

when you are using single quotes use only double qoutes in it or vice versa as

echo '
 <tr class="table-body" onclick="showMenu("edit-user-window")">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>

'

or

echo "
 <tr class='table-body' onclick='showMenu('edit-user-window')'>
        <td> <input type='submit' class='delete-but graphic-buts' value='' /> </td>
        <td> <a href='/res/doc.docx' target='_blank'> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>

"
Null
  • 154
  • 1
  • 14