I am trying to register a WordPress shortcode.
I have a PHP return statement that includes HTML. Inside the HTML, I have a javascript onclick function. Visual Studio Code is throwing a syntax error which is, unexpected 'frame' (T_STRING), expecting ',' or ';'
I have read other articles on Stack Overflow and I have tried escaping single quotes that are inside the string but I could be inaccurately escaping. Below is some raw code without any escaping.
I understand the code below may not be pretty, but all help is appreciated nonetheless.
<?php
function torque_hello_world_shortcode() {
return '<div class="description pagecontent simple"></div>
<a onclick="document.getElementById('frame').style.display = document.getElementById('frame').style.display == 'none' ? 'block' : 'none'; return false;"><img class="someclass" src="#" alt="alt text" style="width:60px;height:60px;cursor:pointer !important;">
<p class="something" style="cursor:pointer !important;">text! <span class="otherclass" style="cursor:pointer !important;">more text</span></p></a>
<div class="description pagecontent simple"></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
You're mixing single and double quotes in your return statement (on line 2 of the string, you basically close the string and follow it with the word "frame", without using concatenation or even the $ variable sign).
If you open a string with single quotes, a second single quote will close the string. If you need to use single quotes within your string, you need to escape it, with a backslash: echo 'Arnold once said: "I\'ll be back"';
I added backslashes to your code:
<?php
function torque_hello_world_shortcode() {
return '<div class="description pagecontent simple"></div>
<a onclick="document.getElementById(\'frame\').style.display =
document.getElementById(\'frame\').style.display == \'none\' ? \'block\' :
\'none\'; return false;"><img class="someclass" src="#" alt="alt text"
style="width:60px;height:60px;cursor:pointer !important;">
<p class="something" style="cursor:pointer !important;">text! <span
class="otherclass" style="cursor:pointer !important;">more text</span></p></a>
<div class="description pagecontent simple"></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
Related
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'>
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 have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.
'.{$row['MemberName']}.'';?>
";?>
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\home - Copy\membercopy.php on line 141
I really don't know where it went wrong. Please help,
<?php
echo '<label onclick="window.open('profilephp.php?member=$row['MemberID']','mywindow')">'{$row['MemberName']}.'</label>';
?>
If you look at that line, you'll see you have your single quoted string with single quotes inside it. Also, you're trying to use variables inside a single quoted string, which doesn't work. You want to change this to:
echo "<label onclick=\"window.open('profilephp.php?member={$row['MemberID']}','mywindow')\">'{$row['MemberName']}.'</label>";
Notice I've double quoted your string and then escaped, with a backslash, any double quotes inside the quoted string.
I also added {} around the first complex variable in the string, since it will give you an error without it.
The error are the non-escaped single quotation marks and the bracets. You write this:
<?php echo '<label onclick="window.open('profilephp.php?member=$row['MemberID']','mywindow')">'.{$row['MemberName']}.'</label>';?>
but is has to look like this:
<?php echo '<label onclick="window.open(\'profilephp.php?member='.$row['MemberID'].'\',\'mywindow\')">'.$row['MemberName'].'</label>';?>
I hope that is what you needed.
This fixes most of the problems with your code (and it's even readable!):
<td style="text-align: center; background-color: #FFFFFF;">
<label onclick="window.open('profilephp.php?member=<?php=$row['MemberID']?>','mywindow')">
<?php=$row['MemberName']?>
</label>
<br />
<img src="<?php=$row['MemberImg']?>" width="100" height="100" alt="" />
</td>