PHP variable as javascript popup [duplicate] - php

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>

Related

How to get variables to output in HTML via an echo in PHP [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
<?php
$request_uri = $_SERVER['REQUEST_URI'] ;
$path=explode("?",$request_uri);
$pname=basename($path[0]);
if ($pname == "blood-facts-for-kids.html") { $p1 = 'Human Body Facts'; $p1u = 'https://www.factsjustforkids.com/human-body-facts.html'; $p2 = 'Blood Facts'; $p2u = 'https://www.factsjustforkids.com/human-body-facts/blood-facts-for-kids.html'; }
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"{$p1}","item":"{$p1u}"},{"#type":"ListItem","position":2,"name":"{$p2}","item":"{$p2u}"}]}</script>';
?>
I'm having issues getting the variables to appear in my echo. Everything works as it should, the variables are set IF the web page name is correct and if I echo out the variables by themselves using
echo "{$p1}, {$p1u}, {$p2}, {$p2u},";
The correct data is shown. I'm obviously doing something wrong in the echo code.
For reference, this is a crude method to inject structured data dynamically.
Either use echo with double quotes "":
echo "<script type=\"application/ld+json\">{\"#context\":\"https://schema.org/\",\"#type\":\"BreadcrumbList\",\"itemListElement\":[{\"#type\":\"ListItem\",\"position\":1,\"name\":\"{$p1}\",\"item\":\"{$p1u}\"},{\"#type\":\"ListItem\",\"position\":2,\"name\":\"{$p2}\",\"item\":\"{$p2u}\"}]}</script>";
or use concatenation:
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"' . $p1 . '","item":"' . $p1u . '"},{"#type":"ListItem","position":2,"name":"' . $p2 . '","item":"' . $p2u . '"}]}</script>';
Notice that with double-quotes, you need to escape any other double quote inside the string.
You can use
echo $variable_name //to display the variable
And if you want to display it in a HTML tag then
<p>Your age is <?php echo $age ?>.</p>
Im providing a link for more detailed information
https://www.dummies.com/programming/php/how-to-display-php-variable-values/

How can i display a username as a link

I am a little new to PHP. What I am trying to do is to display the username that is logged in with a link to their home page. This is what I have.
if (loggedin()) {
echo ' $user->get_fullname($uid) ' ;
echo ' Log Out ';
}
But as you can see it will not display the username, it will display what I have in between the link. I do not know where to go from here.
you want:
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {
?>
in php single quotes will not parse a variable. An alternative syntax is:
echo "<a href ='HomePage.php'> $user->get_fullname($uid) </a>" ;
This is because variables are interpreted inside of double quotes, not single quotes. There are many ways to fix this such as bringing the variable outside of the single quotes as in:
echo ''. $user->get_fullname($uid). '';
or to replace the single quotes with double quotes and vice-versa, and because it is a complex variable you will need to use braces:
echo "<a href ='HomePage.php'>{$user->get_fullname($uid)}</a>";
Use double quotes all throughout and escape the inner quotes:
echo "{$user->get_fullname($uid)}";
And lastly, my favourite way if I have a lot of HTML code is to use HEREDOC syntax:
if(loggedin()){
echo <<<HTML
{$user->get_fullname($uid)}
Log Out
HTML;
}
?>
Note: When using HEREDOC notation, you cannot put anything after the HEREDOC opening variable (not even space) and nothing else (not even space) on the closing HEREDOC line. That is why the closing HTML; is not indented here as no space is allowed before it.
You have to end the string with the quote and continue it to the php variable with the . operator. You concatenate strings with php variables using the .
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {
//do something else
}
if(loggedin()){
$userName = $user->get_fullname($uid);
if($userName == NULL) $userName = 'USER';
if(loggedin()){
echo "<a href ='HomePage.php'>".$userName."</a>";
echo " || <a href='logout.php'> Log Out </a>";
}else{
//if any other then you can...
}
If you want to display some variables in php it should not be enclosed in quotes but if you want a string to display you want it to be inside quotes. So in your case you have both so you must use something called concatenation, For which you use dot(.)[ In case of javascript you use plus(+) for concatenation].
So your code must be like
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {

target=_Blank in this php code [duplicate]

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);

using ' and " in php syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.

php echo a hyperlink with javascript confirm function

I have been using this code for deleting data from database. What i wan is whenever a user clicks an image link to delete data from the confirm function prompts up and ask for action, i am getting error in this code.
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
echo "<a href=".$delurl.";
echo "onclick='return confirm('Are you sure you want to delete.')'>".$img."</a>";
Maybe the error is in double quotes or single quotes, Any help
Thanks in advance
change
echo "<a href=".$delurl.";
to
echo "<a href=\"".$delurl."\" ";
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
$confirm_box <<<CONFIRM
<a href="$delurl"
onclick="return confirm('Are you sure you want to delete?')">$img</a>
CONFIRM;
// then elsewhere ...
echo $confirm_box
Always tend towards using the HEREDOC syntax to construct HTML/JS output, it will save you a lot of heartache. Just watch out for the major gotcha, DO NOT INDENT THE FIRST/LAST lines of the heredoc declaration.
EDIT The benefit being that you can mix single and double quotes as much as you like, you only have to worry about the JS quoting - PHP variables are interpolated without the quotes. You can further wrap curly quotes around your PHP variables like {$this} to make it easier to read, but also to delineate $this and {$this}tle.
I would us the following instead of escaping, this is more readable to me:
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
?>
<?=$img?>
You can, may and should escape when handling stuff like this:
echo "<a href=\".$delurl.\"";
echo " onclick=\"return confirm('Are you sure you want to delete.')\">".$img."</a>";
lg,
flo

Categories