Im trying to add a PHP variable to a href url
This is the code:
PHP
$uid= $_SESSION['userid'];
HTML
<a href=http://example.com/uid= <?php echo ".$uid."?> /a>
How do you add, when I do it, this is what it redirect too: http://example.com/uid=.
Try this,
<?php
#session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a>
echo "link description";
Perhaps something like this:
print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';
try this
<?php
$url = 'https://www.w3schools.com/php/';
?>
PHP 5 Tutorial
Or for PHP 5.4+ (<?= is the PHP short echo tag):
PHP 5 Tutorial
or
echo 'PHP 5 Tutorial';
This is one of the most helpful
echo "<td>".($tripId != null ? "<a target=\"_blank\"href=\"http://www.rooms.com/r/trip/".$tripId."\">".$tripId."</a>" : "-")."</td>";
It will work!
This will work
<a href=http://example.com/?uid= <?php echo $uid ?> Myurl</a>
Try this:
<a href=http://example.com/uid= <?=$uid?> /a>
You're doing a couple things wrong.
In HTML you should quote attribute values such as href
In PHP you're not concatenating anything, just echoing
You forgot the link text
For readability, you can (probably) use the <?= shorthand instead of <?php echo
<a /a> is broken tag syntax. Should be <a>...</a>
End result is this:
Link Text
Related
I have a anchor tag that links to a php page, I would like to use special characters in the url parameter title
The string I would like to pass in the url:
Which is better approach to use href="#" or href="javascript:void(0)"
My anchor tag href will look like this:
mypage.php?title=Which is better approach to use href="#" or href="javascript:void(0)"
on my php page, when i echo $_GET["title"]; I only get part of the string:
Which is better approach to use href="
how to display exact title which i used in my anchor tag
You have to enconde the string before sending it to the title variable.
Example:
$title = 'Which is better approach to use href="#" or href="javascript:void(0)"'
echo '<a href="mypage.php?title='. urlencode($title) . '">';
// and to get the title
echo urldecode($_GET["title"]);
Use urlencode() to output your links and urldecode() to echo them in the other page:
First page:
<?php
$link = urlencode('Which is better approach to use href="#" or href="javascript:void(0)"');
echo 'a link';
?>
And on mypage.php you'll do:
<?php
echo urldecode( $_GET['title'] );
?>
The PHP $_GET['title'] is the variable "title" of the get array which are in fact the variables after the ? in an url.
test
this would show TheTitle
But you'll probably want to url escape the title.
Link
I am setting the value of a php variable to some html. i.e.
$_img = 'hehehehehe';
The variable is then shown in html after a br tag. But it doesn't execute the html in it. Rather it displays it like hehehehehe. So, is there any problem in my code! How can i do this thing?
Here is the code that displays that IN HTML,
<?php if ($_item->getComment()): ?> <br/><?php echo $this->escapeHtml($_item->getComment(), array('b','br','strong','i','u')) ?> <?php endif; ?>
<?php
$string = 'Hehehe';
echo $string;
?>
This works fine! The html is 'executed' and the link is displayed.
From your comment....
Here is the code that displays that <?php if ($_item->getComment()):
?> <br/><?php echo $this->escapeHtml($_item->getComment(),
array('b','br','strong','i','u')) ?> <?php endif; ?>
As predicted by many people, it looks like you are encoding the value when you display it.
I don't know what the $this->escapeHtml function is doing exactly, but it would appear to be doing an HTML Encoding on the string.
The result being that any tag, for example <a> will be sent to the browser as <a> which the browser will display as <a>. The browser will not see it as a tag, and will therefore not treat it as one.
So the simple answer is: don't encode the HTML...
<?php echo $_item->getComment(); ?>
I suspect you are just echoing the variable.
You need use the 'htmlspecialchars' method such as below.
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
Is there a better way to show hyper links in php than using
<?php echo $link; ?>
To avoid repeating $link, you can use this:
<?php printf('%1$s', $link); ?>
<?=$link?>
<?php echo ''.$link.''; ?>
There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.
I would like to set values on an url like this:
<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'¬e='$id_note'&user='$username'>
Then be able to grab then from the recieving page.
Right now all im getting is this when clicked:
http://localhost/readnotes/viewyournote.php?chapter=
I don't how you embed your link in your code, but if it is outside of <?php ?> tags, then you have to do:
<a href="http://<?php echo $data_url ?>/viewyournote.php?chapter=<?php echo $name_of_chapter ?>¬e=<?php echo $id_note ?>&user=<?php echo $username?>" >
if it is inside these tags, you can also do:
echo "<a href='http://$data_url/viewyournote.php?chapter=$name_of_chapter¬e=$id_note&user=$username?' >";
You can get these values on the recieveing page with $_GET['variable_name_here'], e.g. $_GET['chapter'].
Use Query string
$val = "yourvalue" ;
$url "http://localhost/readnotes/viewyournote.php?chapter=$val";
Now $val is passed to specified url .
There you could get it by using $_GET['chapter'] , It will give you "yourvalue"
<a href='http://$data_url/viewyournote.php?chapter=<?php echo $name_of_chapter; ?>¬e=<?php echo $id_note; ?>&user=<?php echo $username; ?>>
Replace your line with
<?php
echo "<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'¬e='$id_note' user='$username'>";
?>
On the receiving end use
<?php
$name_of_chapter = $_GET['chapter'];
...
?>