I'm trying to get a URL to have a dynamic parameter based on the value of a variable.
Here's what Im doing
echo "The file name is ".$value.''.$value.''."<br>";
However, when I click on the corresponding link I get the following URL in the browser
http://localhost/HelloWorld/filespecificpage.php?filename=<?php echo $value ?> rather than the actual value in $value. Any help on how I can fix this would be appreciated.
PHP Noob here, appreciate the patience.
You're already in PHP script mode, you don't need <?php. You just need to concatenate the variable, like you did earlier in the line. You should also use urlencode() when substituting a variable into a URL parameter.
echo "The file name is ".$value.''.$value.''."<br>";
If you break the PHP String/Echo dont use the doublequotes, it makes your live much harder as you need ist.
For your question
echo 'The file name is ' . $value . '<a href="filespecificpage.php?filename=' . urlencode($value) . '>' . $value . '</a><br>';
An expample why single and not doublequotes. If you write an Hyperlink in HTML you use
Link description
and all is fine.
If you do it in an PHP echo with doublequotes you must escape all quotes.
echo "Link description";
Perfect look and way
echo '<a href="website" target="_blank" ' . $anyPHPvar . '>Link description</a>';
and you can use " for clear html and ' for PHP ;)
So, use singlequotes makes your life easyer and its a little bit faster and welcome to PHP ;)
Related
I am trying to format a mailto link that works with a variety of mail clients. This works with Mac's "Mail" and Thunderbird. However, when I click the link on my Android phone, the query string in the URL is stripped starting with the equals sign. I have tried to add code to specify the content type, to parse the query string and re-add it, etc. So far, nothing works. Here's my code:
<?php
ini_set('default_charset', 'UTF-8');
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=I found this article worthwhile and think you will too:%0D%0A %0D%0A
https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . "?" . htmlspecialchars($_SERVER['QUERY_STRING']) . "%0D%0A";
echo '">share with a friend</a></li>
';
?>
I'm pretty proficient in html/css but a beginner with PHP, so I am not at all sure about the efficacy of some of the PHP I added. I'd greatly appreciate any help.
I seem to have found the solution to the problem, which I'm posting below. I've removed the code shared in my initial post that didn't help (in case you're wondering, the nonbreaking space after the colon is for Thunderbird, which does not honor the new line code).
The solution was to encode the = sign. In order to do that, I first extracted the first and last parts of the URL: (1) the first part without the query string and (2) the query string after the = sign. Then I concatenated it back together, spelling out the middle part between the main URL and the unique code at the end of the query string — the question mark (encoded as %3f), the query code identifier (IDN), and the equal sign (encoded %3d). This solution passes the entire URL, including all of the query string, to my gmail client when I'm using my Android phone.
<?php
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
$IDEN = ltrim(($_SERVER['QUERY_STRING']), 'IDN=');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=%0D%0AI found this article worthwhile and think you will too:
%0D%0A%0D%0A' . htmlspecialchars("\n\r") . 'https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . '%3fIDN%3d' . $IDEN;
echo '">share with a friend</a></li>
';
?>
I have a php variable that is created like so:
$scholarshipTitleOutput = ($scholarship['field_sec1_title']['#items'][0]['value']);
If I print the variable by itself it renders correctly, but I want the output to be placed between two header tags. What I would expect to be able to do it echo out the following...
echo "<h2>" . $scholarshipTitleOutput . "</h2>";
But this removes the variable output from the page for some reason. I've tested further and found that I can concatenate after echoing the variable to do things like add a space...
echo $scholarshipTitleOutput . " ";
So what is it about that first example that is completely removing the variable from outputting on the page?
It works fine when I try it. You can also try
echo "<h2>{$scholarshipTitleOutput}</h2>";
I have a database built and working for my music collection. I have built an entry page that only I have access to. Obviously when I have a title or artist with an apostrophe the entry fails. I've been reading and I found this syntax on a page on this site. However when I try to execute it the 'safe' stringer comes out blank.
This is the part of the code that doesn't work. I'm sure it's something stupid I've done. If anyone can point out the error of my ways I'd greatly appreciate it.
$artist=$_POST['artist'];
echo $artist . "<br>";
$safeartist = mysqli_real_escape_string($artist);
echo $safeartist . "<br>";
$title=$_POST['title'];
$safetitle = mysql_real_escape_string($title);
echo $safetitle . "<br>";
It does in fact have the data in the 'echo artist' command but does not for the second echo.
If you are trying to print the string in the DOM it might be that you are looking for htmlentities
http://www.php.net/htmlentities
mysql_* is deprecated PDO is often a more reasonable choice http://php.net/PDO
I'm current making a website with a photo album in it. The website in 2 languages, english and dutch. So I made language file like:
$lang['hello'] = 'Hallo'; //Hallo is hello in dutch
With the photo album I'm trying use the same principle like:
$lang['discription_001'] = 'photo of a house';
With showing the images I made a counter, now I want to use the same counter in the dispription like so:
echo $lang['discription_'$counter]
And $counter being 001 for photo number one. However this does not work, Could someone tell how I could get this to work, or any other method to get what I want.
Thanks in advance, Thomas de Zeeuw
P.S. I'm new in PHP, however I normally pick up things quite fast, so make some explaintion would be appreciated.
You're almost there:
echo $lang['discription_' . $counter]
The . is PHP's concatenation operator, for combining strings.
You just forgot the string concatenation operator . to concatenate 'discription_' and the value of $counter:
$lang['discription_'.$counter]
You have a typo.
echo $lang['discription_'$counter]
Should be
echo $lang['discription_' . $counter];
You could get it to work by simple repairing your code:
echo $lang['discription_'.$counter];
or
echo $lang["discription_{$counter}"];
Is $counter a string?? If so your example should work fine if you fix the parse error (You missed the concatenation operator (.).
It would be much better if you showed us actual code from your application.
echo $lang['discription_' . $counter];
As you have got answers already you forgot the concatenation operator, but better way in my mind would be to use multidimensional array.
echo $lang['descriptions'][$counter];
Am trying to pass the value hrough the URL but when i retrieve it it appears like this ' , urlencode(15.99),'
the value is correct but i have tried different things but still unable to just send the value without the urlencode or added syntax
the line of code am trying to send the value is
redirect_to("$the_file_is?subj=' . urlencode($the_price_is)' ");
This should do what you're looking for, assuming I understood it correctly:
redirect_to($the_file_is . '?subj=' . urlencode($the_price_is));
Surely just change to...
redirect_to($the_file_is."?subj=" . urlencode($the_price_is));
Almost there - just a little bit of quote confusion :)
redirect_to("$the_file_is?subj=" . urlencode($the_price_is));
It's worth noting that you can reference variables in double quotes so:
echo "hello $name"; will work, but echo "$actioned"; might need to be echo "{$action}ed"; echo $action.'ed'; depending.