简体   繁体   中英

Double quotes ,single quotes problem in javascript of html tag in php echo statement

I am using echo statement and onchange of select tag I have used javascript

    echo('<select name="type"  onchange="(return location.href='.?&amp;class=.'+this.value;)" id="manage_ads">
        <option value="ads">Paid To Click Ads</option>
        <option value="banner_ads">Banner Ad</option>
        <option value="featured_ads">Featured Text Ads</option>
        <option value="featured_link">Featured Link Ads</option>
    </select>');

It is showing error as href=' here single quote is conflicting with echo statement.

but if I use it in another javascript page then the function doesn't work properly as it gives error of classundefined

function check()
{
    return location.href='.?&amp;class=.'+this.value;
}

so is there any way with which I can use this function

These are typically perfect scenarios for jumping into and out of PHP mode:

?>
<select name="type" onchange="(return location.href='.?&amp;class=.'+this.value;)" id="manage_ads">
    <option value="ads">Paid To Click Ads</option>
    <option value="banner_ads">Banner Ad</option>
    <option value="featured_ads">Featured Text Ads</option>
    <option value="featured_link">Featured Link Ads</option>
</select>
<?php

While inoften used, this strategy can even be implemented in the scope of functions and methods:

function createSelectControl($name, array $options) {
    ?>
    <select name="<?= $name ?>">
        <?php 
            foreach ($options as $name => $value) {
                createSelectOption($name, $value);
            }
        ?>
    </select>
    <?php
}

function createSelectOption($name, $value) {
    ?>
        <option value="<?= $value ?>"><?= $name ?></option>
    <?php
}

The point being: don't echo copious amounts of HTML ( or anything for that matter ) with PHP. It'll save you from the inevitable quote mismatch issues that follow.

you forgot to escape them

echo('<select name="type"  onchange="(return location.href=\'.?&amp;class=.\'+this.value;)" id="manage_ads">
        <option value="ads">Paid To Click Ads</option>
        <option value="banner_ads">Banner Ad</option>
        <option value="featured_ads">Featured Text Ads</option>
        <option value="featured_link">Featured Link Ads</option>
    </select>');

Try this, In PHP you have to escape single quotes using backslashes

echo('<select name="type"  onchange="(return location.href=\'?&amp;class=\'+this.value;)" id="manage_ads">

<option value="ads">Paid To Click Ads</option>
<option value="banner_ads">Banner Ad</option>
<option value="featured_ads">Featured Text Ads</option>
<option value="featured_link">Featured Link Ads</option>
</select>');

你可以像这样逃避单引号:

echo('<select name="type"  onchange="(return location.href=\'.?&amp;class=.\'"+this.value;)"  ...

Escape the single quotes you want to have as literals in a string. Like this:

echo('<select name="type"  onchange="(return location.href=\'.?&amp;class=.\'+this.value;)" id="manage_ads">
            <option value="ads">Paid To Click Ads</option>
            <option value="banner_ads">Banner Ad</option>
            <option value="featured_ads">Featured Text Ads</option>
            <option value="featured_link">Featured Link Ads</option>
        </select>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM