[英]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语句和选择标签的onchange我使用了javascript
echo('<select name="type" onchange="(return location.href='.?&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 doesn't work properly as it gives error of classundefined 但如果我在另一个javascript页面中使用它,那么该函数无法正常工作,因为它会给出classundefined的错误
function check()
{
return location.href='.?&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: 这些通常是跳进和退出PHP模式的完美场景:
?>
<select name="type" onchange="(return location.href='.?&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. 关键是:不要用PHP
echo
大量的HTML( 或任何相关的东西 )。 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=\'.?&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 试试这个,在PHP中你必须使用反斜杠转义单引号
echo('<select name="type" onchange="(return location.href=\'?&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=\'.?&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=\'.?&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>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.