PHP Variable as HTML Link with PHP Function as HREF [duplicate] - php

This question already has answers here:
How do I run PHP code when a user clicks on a link?
(10 answers)
Closed 9 years ago.
Here is the code:
$vote_up = 'Vote Up';
I am simply trying to have my PHP function insert a link that has a line of PHP as the href. But it does not work right. How can I do this?
Quick update: I got some good answers, however I'd like for the function to fire when clicked (hence the need for the <?php tags).

You're trying to use PHP tags inside the string context. They have no meaning and hence won't produce the output you need.
Simply use string concatenation:
$vote_up = 'Vote Up';
Or use sprintf() (bit more cleaner):
$vote_up = sprintf('Vote Up', amc_comment_vote("up"));

this is also a possibility:
$vote_up = "<a href='".amc_comment_vote("up")."'>Vote Up</a>";

Related

Why echo is necessary in php inside html files, in this example? [duplicate]

This question already has answers here:
What is the difference between get_the_* and the_* template tags in wordpress?
(3 answers)
Closed 4 years ago.
I wonder:
why I this code is valid:
go to this post
but for retrieving the following value I must use echo, or else it won't work:
back to homepage
I took a glance at get_option documentation and it said that:
Return# - (mixed) Value set for the option.
So perhaps that is the different, that the return value of this function is not a string?
the_permalink() calls echo within the function call. See the full source here. get_option() only returns a value so you have to echo it explicitly if you want it in the html.
I' m not sure, but if you review the the_permalink(); method's body you may see echo command at the end of the method while get_option('home'); just return a string as the result.

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

How to Use a Variable which is a URL and pass it to a href tag so it opens as a Hyperlink in HTML code [duplicate]

This question already has answers here:
Insert php variable in a href
(4 answers)
Closed 7 years ago.
var= www.google.com
Now I want to pass this variable to an a href tag, how shall I do it?
I tried doing below but it didn't work. I need to pass it as variable since every time the url is going to be different.
NoAbTerm
Appreciate your any help.
If you want the variable as php, you could do this:
$whatevervariable = "http://www.google.com";
/* then, you can echo it out */
echo"
<a href='".$whatevervariable."'>google</a>
";

how to extract data from mysql that contains special characters? [duplicate]

This question already has answers here:
php echoing angle brackets
(4 answers)
Closed 9 years ago.
Example data in my database:
blabla<blabla
I use phpmyadmin and can see that the data has been input successfully.
However when I try to display the data what I get is:
blabla NOT blabla<blabla
In other words, everything after the < symbol does not display.
<?
while ($mouselist_row = mysql_fetch_array($mouselist)) {
$mouselist_commonstrain = mysql_real_escape_string($mouselist_row['Common_Strain']);
echo "$mouselist_commonstrain.";
}
?>
I tried using mysql_real_escape_string.
Is there something in particular needed to display the <?
thanks
You want something like:
echo htmlspecialchars($mouselist_commonstrain);
(It needs to be HTML escaped.)
try this
$mouselist_commonstrain = stripslashes(htmlspecialchars($mouselist_row['Common_Strain']));
Your problem isn't escaping SQL but HTML. As answered in this question you can use htmlspecialchars function.

Passing PHP variable in a javascript function [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
<button class="addToPlaylist" onclick="javascript:myPopup(<?php echo $videos[$counter]?>);
return false;">+</button>
I have a button on an image as a html hyperlink. I want to perform different actions on hyperlink and button. The above code works whenever I do not pass the PHP variable using echo. When i pass PHP variable, the button also performs the same action as of the hyperlink, that means return false does not work.
Any idea why the return false; does not work when i pass PHP variable?
This should be:
<button class="addToPlaylist" onclick="javascript:myPopup('<?php echo $videos[$counter];?>');return false;">+</button>
Note the single quotes in myPopup. As you pass a string to myPopup, you will need to enclose it with single quotes. (Double won't work as there is already double quotes for the onclick)
I am quite sure $videos[$counter] is not numeric, but a string. In this case you have to write the quotes:
onclick="javascript:myPopup('<?php echo $videos[$counter]?>');
And make sure, $videos[$counter] doesn't contain any, something like
onclick="javascript:myPopup('<?php echo addslashes($videos[$counter])?>');
comes to mind.
onclick="javascript:myPopup("";return false;" . This should work and i think it's more clear where you have javascript code and php code.

Categories