PHP How to correctly echo a link? - php

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>

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>"

How to pass parameters in url using php page

here i am trying to pass the id as parameter for next page using php.
I am retrieving values from database and i have id as a primary key. i want to pass the id for next page. but when i pass the parameter through URL its not appearing on url. how can i do this?
here is what i have done.
echo "<a href='product-des.php?'". $row["product_id"].">";
here the URL is visible till.php but i am not able to see any id after that.
what am i doing wrong here... how can i do this?
To pass parameter
echo '<a href="product-des.php?pro_id='. $row["product_id"].'">';
To get parameter:
echo $_GET['pro_id'];
this
echo "<a href='product-des.php?'". $row["product_id"].">";
should be like this
echo "<a href='product-des.php?product_id=".$row['product_id']'>."'" >Product Name</a>";
try this

PHP How to pass URL parameter into another URL

I have a URL with a parameter
www.url.com/page.php?n=name
Now, within a page.php I need to create a hyperlink that works with that parameter. I now have
<a href="page2.php?a=param2&n=<?php $_GET["n"] ?>" >
but this does not work and the URL shows with empty parameter "n":
http://www.url.com/page2.php?a=param2&n=
Could you please help me to pass that parameter in the hyperlink? Kindly thank you in advance.
You need to do <?php echo $_GET["n"] ?>
The echo actually prints it into the code. You were just calling it.
You need to echo the PHP out so it is echoed out to the HTML, and is then in the source code.
Like so:
<a href="page2.php?a=param2&n=<?php echo $_GET["n"] ?>" >

Dynamic Link inside 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

Use both ahref and onclick on php to hIde parameters?

I have a php function which gets data from database and calls a javasript function like this:
echo "<p>$header</p>";
The problem is when someone hovers over the link, the browser shows the complete parameters like this:(on bottom left of the browser, or a new tab)
javascript: customm('some secret code here');
Is there a way to hide this? I tried adding onclick to the php function and pointing ahref to #.
echo "<p><a href='#' onclick='javascript: customm('$variable')'>$header</a></p>";
But it didnot work.
Thanks in advance!
It's not working because you need to escape the quotes:
echo "<p><a href='#' onclick='customm(\"$variable\"); return false;'>$header</a></p>";
This code worked:
echo "<p>$header</p>";
Thanks KaeruCT and Blender!

Categories