target=_Blank in this php code [duplicate] - php

This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
Closed 8 years ago.
Hope you're all well.
So here's what I want to do. I want to add to a review plugin in wordpress the possibility to open the page I want in a new window with the target="_blank" code.
I believe that's where the magic is happening, this is the original:
if ($show_morelink != '') {
$review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."'>$show_morelink</a>";
}
This is what I did without any success:
if ($show_morelink != '') {
$review->review_text .= " $show_morelink";
}
I'm a beginner in PHP and I hope that someone can help me with this... I know it's not so hard.. I'm just missing something.
Thanks!

You must escape your quotes.
Use the following
$_morelink != '') {
$review->review_text .= "$show_morelink";
}
Source for handling strings.

Because your code is surrounded with double quotes, you are breaking out of them when you add in the target. You can either escape the quotes like this using a slash:
$review->review_text .= " $show_morelink";
Or change to using single quotes:
$review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target='_blank'>$show_morelink</a>";
Edit
A third way you could do it is surrounding the whole string in single quotes and remove the single quotes and periods form inside:
$review->review_text .= ' $show_morelink';

Your problem is that you're using doublequotes to denote php strings, so you can't use doublequotes for your html:
if ($show_morelink != '') {
$review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target='_blank'>$show_morelink</a>";
}
If you look at the HTML output you will see that both the href and target use single quotes now.

I always prefer to use single quotes for HTML code strings to improve readability.
if ($show_morelink != '') {
$review->review_text .= '
'.$show_morelink.'';
}

Lots of answers; most correctly pointing out the incorrect escaping of the quotations.
As it has not been mentioned yet sprintf() can also help with readability rather than having to concatenate strings.
$link = $this->get_jumplink_for_review($review,1);
$text = sprintf('%s', $link, $label);

Related

handle single quote and double quote in jquery function

I have created one link from foreach loop in that I am showing one link with different parameters in jquery function
<?php
foreach ($questions as $row) {
if (!empty($row['url_ImageName'])) {
$url_ImageName = $row['url_ImageName'];
}else{
$Paragraph = $row['Paragraph'];
}
?>
Show Details
<?php
} ?>
function question_details(url_ImageName,Paragraph){
if (url_ImageName != '')
{
$(".exam-slideout .question-details img").attr("src",url_ImageName);
}
if (Paragraph != '')
{
$('.exam-slideout .question-details div').html(Paragraph);
}
}
in that first link which is created this:
Show Details
and the second link which is created this:
Show Details
in that, I have facing an issue with single quotes and double quotes.
to resolve this issue I have a try
$Paragraph = mysqli_real_escape_string($con, $row['Paragraph']);
But still function is not working with syntax error.
can anybody help me in this.
Just add a escape character (\) before the ' used in the middle of the string like:
Show Details
Alternative using Template Literals:
Show Details
You can learn more about Working with Strings in JavaScript.
You can use addslashes() on the $Paragraph variable, this will escape ' into \'. It will also escape ", so be a bit wary of it.
Show Details
Alternatively, replace all occurrances of ' to \' using str_replace().
Show Details
Live demo at https://3v4l.org/IFLnY

php binding quotes in html

I have following code in php where I am binding html code:
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(".$base_image_url.$myDoc->my_doc_name.")' value='".$myDoc->my_doc_id."' class='cropcss'/>";
It produces following output:
<img src="mysite.in/8422_1477013411.png" onclick="window.open(mysite.in/8422_1477013411.png)" value="623" class="cropcss">
In window.open(mysite.in/8422_1477013411.png) I have missed single quote inside. How do I add this?
I prefer to use single quotes for outputting variables like HTML to the browser. Simply use \' to escape a single quote, and pass it to the browser. Something like this should work:
$my_doc .= '<img src="mysite.in/' . $myDoc->my_doc_name . '" onclick="window.open(\'' . $base_image_url . $myDoc->my_doc_name . '\')" value="' . $myDoc->my_doc_id . '" class="cropcss" />";
If you insist on using double quotes, a minor quick change can fix this (Just add \" around your text):
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(\"".$base_image_url.$myDoc->my_doc_name."\")' value='".$myDoc->my_doc_id."' class='cropcss'/>";
For more info, see this post on how to escape quotation marks in php
As an alternative - you can use HEREDOC syntax - keeps it nice and neat in the output code, no need to worry about escaping quote marks or breaking/concatenating the string.
$my_doc .= <<<MYDOC
<img src="mysite.in/{$myDoc->my_doc_name}" onclick="window.open('{$base_image_url}{$myDoc->my_doc_name}')" value="{$myDoc->my_doc_id}" class="cropcss" />
MYDOC;
check code replace this
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open('".$base_image_url.$myDoc->my_doc_name."')' value='".$myDoc->my_doc_id."' class='cropcss'/>";

Single quote within single quotes PHP

I have a HTML achor tag like below:
echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];
And the corresponding javascript function tempBuy() is
function tempBuy(rate,veg_name,market_name,dt)
{
alert(dt);
}
But the problem is it does not alert at all ! May be I need to include the variable names within single quotes in tempBuy() function. I tried tempBuy(\'var1'\,\'var2\'...) but it shows error. How can I able to to that. Thanks .
Source for the part shows like this:
<td width="120px" class="">56.0
</td>
<script>
function tempBuy(rate,veg_name,market_name,dt)
{
alert(rate);
}
</script>
You didn't wrap your javascript arguments in quotes. You need to wrap each variable in single quotes, since you used double quotes for "href" attribute. Another thing is that you didn't close up "a" HTML tag.
echo ''.$res_get_price[0][0].'';
If there is anything in your variables that is not a valid javascript literal you have to make it a string like:
echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...
If there are ' in your variables you have to replace them with \' as well.
As you can see form the rendered output, you need to quote the last 3 arguments which are non-numeric. The correct output should be: javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')
The corrected PHP code is:
echo ''.$res_get_price[0][0].'';`
echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];

PHP variable as javascript popup [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating javscript function to destroy php session
Hi I am trying to create a php variable that will display itself as a popup. Here is the code I have...
<?php
// this starts the session
session_start();
$var = "";
// echo variable from the session, we set this on our other page
if ($_SESSION['color'] == "") {
$var = "<a href='JavaScript:newPopup('http://www.yourfantasyfootballreality.com/signin.php');' class='two'>Sign In</a>";
} else {
echo "Hello, ";
}
echo $var;
?>
I can't seem to arrange the semicolons and quotes correctly. Can someone please show me how this is done.
You can escape the quote character you are using to delimit your string within the string with the escape character (\)...
$var = "Sign In";
Alternatively, you can mix quotes, i.e. use single quotes to delimit your string and double quotes for quotes around your attributes or vice versa.
You could also use heredoc or nowdoc.
In "<a href='JavaScript:newPopup('http://www.yourfantasyfootballreality.com/signin.php');' class='two'>Sign In</a>"; the 2nd ' closes the href tag. You need to escape it, so you would do something like:
$var = "<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
So that when it actually outputs you get <a href='JavaScript:newPopup("http://www.yourfantasyfootballreality.com/signin.php");' class='two'>Sign In</a>

How to PHP echo when using multiple quotes

How should I quote this:
<tr onclick="$.colorbox({href:'information1.html'});">
When put in an echo " "; ?
I have tried this:
echo "<tr onclick='$.colorbox({href:'information1.html'});'>";
Which shows a Jquery error.
And I tried this:
echo "<tr onclick="$.colorbox({href:'information1.html'});">";
Which shows a PHP error.
Any workarounds? Thanks
You need to escape the quotes symbols:
echo '<tr onclick="$.colorbox({href:\"information1.html\"});">'
Note that using inline script is not considered to be a good practice!
echo '<tr class="foo">'
In the javascript code:
$('.foo').click(function() {
$.colorbox({ href: "information1.html" });
});​
Simply escape the quotes. Whilst on this subject I feel it important to mention the fact that generally speaking, you should use single quotes for 'code' and double quotes only for displayed strings.
This stems from C standards and keeping this consistent will help you in the future if for example you wanted to implement gettext() and translate your website into multiple languages.
echo '<tr onclick="$.colorbox({href:\'information1.html\'});\">';
Having said that, there's a better way to achieve what you're doing. Give the row an id:
<tr id="inforow" />
And use jQuery to bind to it's click event when the DOM is ready.
$(document).ready(function() {
$(".inforow").click(function() {
$.colorbox({href:'information1.html'});
});
});
Anytime you want to print a string with a quote in it, just use the escape character '\' to ignore the quote as a literal closing quote, like so:
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
Try that:
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
I would use PHP-methods instead of caring about the quotes
echo '<tr onclick="'.
htmlentities('$.colorbox('.json_encode(array('href'=>'information.html'))).')">';
...will always create proper JSON and proper HTML, no matter what characters you use.
NO NEED to quote it.
NO NEED to put in an echo " ";
Just leave it AS IS:
?>
<tr onclick="$.colorbox({href:'information1.html'});">
<?
as well as any other HTML.
It's PHP. It's embedded in HTML. You can leave PHP mode any time
Another way is using EOD
$string = <<<EOD
"duble quotation" and 'quotation' all enable
EOD;
echo $string;

Categories