Dynamic Link inside PHP - php

I have been toying around with the idea where I echo a link and it changes based on the string I got from my database. (the GET command)
Here is the code I been working with:
<?php echo '<a href="submittoplay.php?username="' . ($username) . ">";?>
I want the output of the echo to appear like so, where $username = bob
<a href="submittoplay.php?username=bob or in the address bar when I click on the link to be submittoplay.php?username=bob
But when I run the code only"<a href="submittoplay.php?=username" shows. It doesnt seem to want to take my string that I had gathered from the database and plug it into the echoed link.
I know $username is a valid string, I tested it and do get the result I am looking for.
Any help would be appreciated!
Thank you!

here it goes :--
$username='bob';
echo 'User link';

inside php
$user_name='sample';
echo 'Your link';
Fiddle Link
Outside Php
Your link
Fiddle Link

Related

How to show a link if user_id =1 in HTML with PHP

I am new and learning PHP and HTML. I have one user table where I am displaying data. I want to show Delete button if user_id = 1. else I want to hide it. My current link code is like below
delete
I have done PHP code for doing achieve my above description
<?php
if ($row['user_id']==1){
echo //// I want a link here
}
?>
But since my link have also used some PHP codes, I am confused and not able to properly echo button. I am trying from an hour and not able to fix it. Let me know if someone can help me for do it.
Thanks!
I think you want to add variables in string:
<?php
if ($row['user_id']==1){
echo 'delete';
}
?>
Use . to combine strings & variables:
'string'.$var
Read about string concatenation in PHP.
Here is a possible solution for your problem:
<?php
if ($row['user_id'] === 1) {
echo ''.$row['username'].'';
}
here an an example...
<?php if ($row['user_id']==1){ ?>
delete
<?php } ?>
you can put html code in echo like this
echo "<tag name></tag name>"

PHP How to correctly echo a link?

What is the correct way to echo a link to another page in php? I have seen this used but it doesn't work?
echo "<p><a href=\"link\">";
Html in PHP
<?php
echo "<a href='$link'>The Link You Want</a>";
?>
Php in Html
Link
It seems like your question was already answered in How to create a link to another PHP page
Index Page
Page 2
If you need to pass a value -
Link that pass the value 1
You would do it like this:
print("<a href='your_target'></a>");
Or if you want to print the code and not the link:
print(htmlspecialchars("<a href='your_target'></a>"));
try this:
<a href='<?php echo "the_page_you_want.php" ?>'>link_text</a>

How to create clickable hyperlink text in custom PHP code

Please can I get some help with this - I have this part of code:
echo '<li><span>'.mg_wpml_string($type, $copt).':</span> '.$val.'</li>';
How to make this part
.$val.
clickable, so In fact how to put this part in a href so that users can click on this?
This is ordinary text ling but all values are entered as http addresses but I can not click on them.
Thank you
Just put the HTML-code for a link (a href) in there...
echo '<li><span>'.mg_wpml_string($type, $copt).':</span> '.$val.'</li>';
Perhaps?
[...snip...]:</span>' . $val . '</li>';

Trying to add html w/ link using echo after else in php

I have content only available to registered users - guests see an intro but if they click to read more are shown a message to login or register - I need to add a link to the registration page in the message they see.
Here is what I have tried:
if (!$user->guest) {
echo $this->item->fulltext;
}
else
echo '<h3><b>Please login or <a herf="/login/register">register</a> to view the entire article</b></h3>';
?>
My text shows correctly & the word register turns blue, but it is not linking.
Please advise. Thank you in advance.
When you can tell me what a herf is, I'll wonder myself.
It's spelled href not herf.
=>
register

link Redirect from php

Dear all
I have database of website link, it list out in main file ,when i try to click that link it get to redirect on that database link.
my code is:
file: test.php
<?php
// getting from database
echo '<li onclick=\"window.location='.$result->website.'\">
'.$result->option.'</li>';
?>
The Main.html calls that test.php
while ajax
$.post("test.php", {queryString: ""+inputString+""}, function(data){
});
how to do it?
any idea Is it possible with serverside script? whats wrong with my php code?
Your code does not work for invalid URLs. www.google.com is not an URL, just a domain name. So skip the silly Javascript links, instead use:
echo "<li>$link</li>\n";
And your Javascript success function seems somewhat empty, so instead use .load() like:
$("ul").load("links.php", {queryString: ""+inputString+""})
Edited after venkat's comment:-
According to your last comment, the code you have problems with is the following:-
<?php
$link="www.google.com";
echo "<a href='#' onclick=window.location='$link'>Click here</a>";
?>
This above code should actually be the following:-
<?php
$link = "http://www.google.com/";
echo 'Click here';
?>
The reason for adding the "http://" string is that the variable "$link" is going to be used as an HTTP URL, which requires mentioning of this "http://" string, mainly because of the protocol to be used by the browser. In this case, the protocol is HTTP.
Always remember that for any URL, there must be a string "http://" at the beginning of the URL string, when stored in either a database / variable.
Coming back to the code in your question, which was:-
<?php
// getting from database
echo '<li onclick=\"window.location='.$result->website.'\">'.$result->option.'</li>';
?>
Now here the position of "window.location" is not totally correct. It should have been in "href" attribute of "a" element, instead of putting it in "onclick" attribute of "li" element.
So the code should actually be:-
<?php
// getting from database
echo '<li>'.$result->option.'</li>';
?>
Hope it helps.

Categories