pufAmuf
pufAmuf

Reputation: 7805

How to print a line of HTML markup containing inline javascript and nested quoting?

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.

Upvotes: 1

Views: 6866

Answers (10)

Gerard St Germain
Gerard St Germain

Reputation: 31

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.

Upvotes: 1

pondol
pondol

Reputation: 939

Another way is using EOD

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

Upvotes: 0

gdoron
gdoron

Reputation: 150303

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" });
});​

Upvotes: 7

Your Common Sense
Your Common Sense

Reputation: 157981

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

Upvotes: 1

deed02392
deed02392

Reputation: 5022

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'});
    });
});

Upvotes: 4

Dr.Molle
Dr.Molle

Reputation: 117354

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.

Upvotes: 1

gzg
gzg

Reputation: 1489

Try that:

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

Upvotes: 1

Kaustubh Karkare
Kaustubh Karkare

Reputation: 1101

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

Upvotes: 1

Stewie
Stewie

Reputation: 3131

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

Upvotes: 1

Kinz
Kinz

Reputation: 310

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'});\">";

Upvotes: 3

Related Questions