1

How should I quote this:

<tr onclick="$.colorbox({href:'information1.html'});">

When put in an echo " "; ?

I have tried this:

echo "<tr onclick='$.colorbox({href:'information1.html'});'>";

Which shows a Jquery error.

And I tried this:

echo "<tr onclick="$.colorbox({href:'information1.html'});">";

Which shows a PHP error.

1
  • 1
    \ escape charecter use like this echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">"; to escape Commented Mar 17, 2012 at 23:27

10 Answers 10

7

You need to escape the quotes symbols:

echo '<tr onclick="$.colorbox({href:\"information1.html\"});">'

Note that using inline script is not considered to be a good practice!

echo '<tr class="foo">'

In the javascript code:

$('.foo').click(function() {
    $.colorbox({ href: "information1.html" });
});​
Sign up to request clarification or add additional context in comments.

1 Comment

@pufAmuf. No problem, And try to avoid usage of inline script, it's deprecated...
4

Simply escape the quotes. Whilst on this subject I feel it important to mention the fact that generally speaking, you should use single quotes for 'code' and double quotes only for displayed strings.

This stems from C standards and keeping this consistent will help you in the future if for example you wanted to implement gettext() and translate your website into multiple languages.

echo '<tr onclick="$.colorbox({href:\'information1.html\'});\">';

Having said that, there's a better way to achieve what you're doing. Give the row an id:

<tr id="inforow" />

And use jQuery to bind to it's click event when the DOM is ready.

$(document).ready(function() {
    $(".inforow").click(function() {
        $.colorbox({href:'information1.html'});
    });
});

Comments

3

Anytime you want to print a string with a quote in it, just use the escape character '\' to ignore the quote as a literal closing quote, like so:

echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";

Comments

1
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";

Comments

1
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";

Comments

1

Try that:

echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";

2 Comments

Thanks gzg, however I got an error after using your code. I had luck with gdoron's answer, but I really appreciate your answer. Thanks!
I edited my answer, I had forgotten the semicolon at the end. :)
1

I would use PHP-methods instead of caring about the quotes

echo '<tr onclick="'.
          htmlentities('$.colorbox('.json_encode(array('href'=>'information.html'))).')">';

...will always create proper JSON and proper HTML, no matter what characters you use.

Comments

1

NO NEED to quote it.
NO NEED to put in an echo " ";

Just leave it AS IS:

?>
<tr onclick="$.colorbox({href:'information1.html'});">
<?

as well as any other HTML.

It's PHP. It's embedded in HTML. You can leave PHP mode any time

1 Comment

Cool, so but what if this data is inside the mysql_fetch_array( $data )). Can I close the php with ?> and then open it back up again without having to do another select query?
1

I've tried multiple ways with escapes:

This worked in html:


But I need it to work in PHP so I've tried the following, but none of these appears to work:

Attempt 1:
echo '<input type="hidden" name="cancel" value="<?php echo $payment->route("https://website.com/cancel.php", "") ?>">';

Attempt 2:
echo '<input type="hidden" name="cancel" value=\"<?php echo $payment->route("https://website.com/cancel.php", "") ?>\">';

Attempt 3:
echo '<input type=\"hidden\" name=\"cancel\" value=\"<?php echo $payment->route(\"https://website.com/cancel.php\", \"\") ?>\">';

Attempt 4:
echo '<input type="hidden" name="cancel" value="<?php echo $payment->route(\"https://website.com/cancel.php\", \"\") ?>">';

Attempt 5:
$val_1='<?php echo $payment->route("https://website.com/cancel.php", "") ?>';

echo '<input type="hidden" name="cancel" value="'.$val_1.'">';

Attempt 6:
$val_1='<?php echo $payment->route("https://website.com/cancel.php", "") ?>';

echo '<input type="hidden" name="cancel" value=\"'.$val_1.'\">';

None of the above attempts worked.

Comments

0

Another way is using EOD

$string = <<<EOD
"duble quotation" and 'quotation' all enable
EOD;
echo $string;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.