I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>
Related
I couldn't get expected answer.How to add php variable in style tag which in echo statement
<div class="container">
<h2>Grantt chart for given process</h2>
<divclass="progress">
<?php $a='80%';
echo '<div class="progress-bar progress bar-success"role="progressbar" style="width: {$a}" >';
echo '</div>';?>
</div
</div>
I expected bar with 80% filled
In your code you are using single quotes, the variable is not interpreted in single quotes.
What is the difference between single-quoted and double-quoted strings in PHP?
you have to use string concatenation or replace your single quotes with doubles and escape the html quotes
echo '<div class="progress-bar progress bar-success"role="progressbar" style="width: '.$a.'" >';
echo "<div class=\"progress-bar progress bar-success\" role=\"progressbar\" style=\"width: {$a}\" >";
I'm new to code igniter and learning with how to display images in html page. But the images displays file but it also displays the image tags alt="" also. Here is my code.
views/admin.php
<div class="row">
<?php
//print_r($images_disp);
foreach($images_disp as $item=>$val){
?>
<div class="col-md-3">
<img src="<?php echo base_url().''.$val['imagepath'];?>" width="150px" height="150px" alt="'<?php echo $val['name']?>'" >
</div>
<?php
}
?>
</div>
controller/AddProduct_controller
public function index(){
$sql['images_disp'] = $this->addProduct_model->show_images();
/* echo "<pre>";
print_r($sql);
echo "</pre>"; */
$this->load->view('admin',$sql);
}
model/AddProduct_model
public function show_images(){
$query = $this->db->get('db_images');
$query = $query->result_array();
return $query;
}
HERE IS THE ISSUE IN BELOW IMAGE
OUTPUT IMAGE
THANKS IN ADVANCE
In your scenario name column contains double quotes(") which is causing problem in echo statement. Double quotes causing alt attribute value issue.
Replace your line of code with this line :
<img src="<?php echo base_url().''.$val['imagepath'];?>" width="150px" height="150px" alt="'<?php echo html_escape($val['name']); ?>'" >
html_escape will escape the html character from your string.
Advice :
Always avoid inserting quotes and other character directly in database, escape user input before inserting in database.
At time of insert escape user input like this.
$escaped_str = $this->db->escape($input_str);
$this->db->escape() method will escape all the quotes from your string.
I have an existing PHP app that generates this div element:
<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>
I am trying to remove above div element from HTML content using PHP, but it doesn't work for me
My php code:
$text = '<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>';
echo preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);
Thanks in your help guys!
You can't use single quotes if your string is enclosed in single quotes.
Either escape them with \ or use different quotes.
$text = "<div style='position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center'>
<div class='asdthjeme'>Designed by
<a href='http://blabla.com/' target='_blank'>dnfdjf</a></div></div>";
or
$text = '<div style=\'position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center\'>
<div class=\'asdthjeme\'>Designed by
<a href=\'http://blabla.com/\' target=\'_blank\'>dnfdjf</a></div></div>';
Try this:
$text = '<div style="position:fixed;left:0;right:0;bottom:0;background:#f00;text-align:center">
<div class="asdthjeme">Designed by
dnfdjf</div></div>';
I am trying to echo some inline CSS using PHP using this:
echo '<div class="image" style="background:url("img/testimage.jpg");width:300px;height:232px;">';
echo '</div>';
But for some reason this is returning this:
<div class="image" testimage.jpg");width:300px;height:232px;"="" img="" style="background:url("></div>
This is within a WordPress environment, am I doing something obvious wrong?
Escape the quotes inside the url declaration properly:
echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
// ^ ^
you can't run something like this (in HTML) correctly:
style="background:url("img/testimage.jpg");width:300px;height:232px;"
must merge between single and duple quotes or escape them:
style="background:url('img/testimage.jpg');width:300px;height:232px;"
solution:
echo "<div class='image' style='background:url(\"img/testimage.jpg\");width:300px;height:232px;'></div>";
echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';
What would be the correct syntax for the onClick attribute in the following:
<?php
if($_resp['user']) {
echo '<div class="logo">Link Text';
}
?>
Since you are nesting both double and single quotes, you will need to escape the single quotes in the onClick with backslashes.
echo '<div class="logo">Link Text';
Consider using a HEREDOC
if ($_resp['user']) {
echo <<<EOL
<div class="logo">Link Text
EOL;
}
or dropping out of PHP mode:
if ($_resp['user']) { ?>
<div class="logo">Link Text
<?php }
Both get the job done, and eliminate any need to escape quotes inside the html/javascript you're generating
You can mix HTML and PHP, so you might want to simply move it out of PHP:
<?php
if($_resp['user']) {
?>
<div class="logo">Link Text
<?
}
?>