Double quotes, single quote problem in javascript html tag in php echo expression

I am using an echo expression and replacing a selection tag. I 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 shows an error since href = 'here the single quote is in conflict with the statement echo.

but if I use it on another JavaScript page, then the function does not work properly, as it throws a classundefined error

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

so is there a way i can use this function

+4
source share
5 answers

Try this, in PHP you need 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>'); 
0
source

These are usually ideal scenarios for entering and exiting from 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 

Despite the fact that this system is often used, this strategy can be implemented even within the framework 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 } 

Point: not echo plentiful amount of HTML (or anything for that matter) with PHP. This will save you from inevitable problems with inadequate quotes.

+1
source

you forgot to avoid 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>'); 
0
source

you can escape single quotes as follows:

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

Discard 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>'); 
0
source

Source: https://habr.com/ru/post/1480250/


All Articles