双引号,单引号在php echo语句中的html标签的javascript中出现问题

时间:2021-08-21 22:29:21

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

我正在使用echo语句和选择标签的onchange我使用了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.

它显示错误,因为href ='这里单引号与echo语句冲突。

but if i use it in another javascript page then the function doenot work properlly as it give error of classundefined

但如果我在另一个javascript页面中使用它,那么函数doenot正常工作,因为它给出了classundefined的错误

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

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

那么有什么方法可以使用这个功能

5 个解决方案

#1


0  

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

试试这个,在PHP中你必须使用反斜杠转义单引号

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

#2


1  

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

这些通常是跳进和退出PHP模式的完美场景:

?>
<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:

虽然使用了inoften,但这个策略甚至可以在函数和方法的范围内实现:

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.

重点是:不要用PHP回显大量的HTML(或任何相关的东西)。它将使您免于接下来不可避免的引用不匹配问题。

#3


0  

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

#4


0  

you can escape the single quotes like this:

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

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

#5


0  

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

#1


0  

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

试试这个,在PHP中你必须使用反斜杠转义单引号

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

#2


1  

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

这些通常是跳进和退出PHP模式的完美场景:

?>
<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:

虽然使用了inoften,但这个策略甚至可以在函数和方法的范围内实现:

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.

重点是:不要用PHP回显大量的HTML(或任何相关的东西)。它将使您免于接下来不可避免的引用不匹配问题。

#3


0  

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

#4


0  

you can escape the single quotes like this:

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

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

#5


0  

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