PHP - setting variable in link?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hbraekke
Forum Newbie
Posts: 5
Joined: Thu Sep 17, 2009 5:03 pm

PHP - setting variable in link?

Post by hbraekke »

Is it possible to set variable as following?

<a href='www.abdefgsite.com/abcde.php?Variablename="my name">text</a>

Is it possible to get -Variablename- in php?

thnx HenningB
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP - setting variable in link?

Post by califdon »

hbraekke wrote:Is it possible to set variable as following?

<a href='www.abdefgsite.com/abcde.php?Variablename="my name">text</a>

Is it possible to get -Variablename- in php?

thnx HenningB
It isn't clear what you are asking, but if you construct that html anchor element in a php script, you can certainly use a variable to insert whatever value you want anywhere you want it in the html that gets sent to the browser. Obviously, that requires that your php script know in advance what value you want to insert. If this isn't clear to you, you probably need to read a basic tutorial on using php. There are many good ones online. Just search using terms like php tutorial basic.
hbraekke
Forum Newbie
Posts: 5
Joined: Thu Sep 17, 2009 5:03 pm

Re: PHP - setting variable in link?

Post by hbraekke »

Yes... I am a beginner in PHP, and are reading books for beginners and search all the web for good tips.

But I hope someone can help me with this one...

If you type the html-adress: ww.mysitename.com/myhtml.php?variablename=henning

I hope the part behind ? (variablename) can be fetched in the php-code.

f.ex:
<?php
$myvar = variablename;

$htmlcode1 = <<<EOD
<html>
<body>
EOD;
echo $htmlcode1

echo $myvar;


$htmlcode2 = <<<EOD
</body>
</html>
EOD;
echo $htmlcode2
?>

I hope someone can help me with this...

thnx HenningB
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP - setting variable in link?

Post by califdon »

I would be happy to help you if I could understand what you are asking. I don't see any connection between your question and the code you posted. And if you post code in the future, please do us the favor of placing [syntax=php]and[/syntax] tags around your code, to format and highlight it, for easier reading.
hbraekke
Forum Newbie
Posts: 5
Joined: Thu Sep 17, 2009 5:03 pm

Re: PHP - setting variable in link?

Post by hbraekke »

I found it out... webbing around...

echo $HTTP_GET_VARS["variabelname"]; //where "variablename="henning" is behind the ? - sign in html-adress

returns henning
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP - setting variable in link?

Post by jackpf »

HTTP_GET_VARS is deprecated.

$_GET is the new thing. 8)
hbraekke
Forum Newbie
Posts: 5
Joined: Thu Sep 17, 2009 5:03 pm

Re: PHP - setting variable in link?

Post by hbraekke »

jackpf wrote:HTTP_GET_VARS is deprecated.

$_GET is the new thing. 8)
I tryed $_GET, but it didnt retrive the variable in the html-adress
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP - setting variable in link?

Post by jackpf »

How odd.

What's your code? And what version of PHP are you running?
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: PHP - setting variable in link?

Post by peterjwest »

$_GET will only work if you are ON that page. The URL bar in your browser needs to have ?Variablename="value" in it. You can't get the variable just from having the hyperlink on that page.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP - setting variable in link?

Post by califdon »

Just what is it that you are trying to do? I think we are all confused about what you are doing.
hbraekke
Forum Newbie
Posts: 5
Joined: Thu Sep 17, 2009 5:03 pm

Re: PHP - setting variable in link?

Post by hbraekke »

jackpf wrote:How odd.

What's your code? And what version of PHP are you running?
If you read my first question... it says... how do I get the variablename in the url. When url is: mysite.com/index.php?variablename=henning

So my question was how to get variablename (=henning) in the phpcode.

Someone has said they use $_GET... I tried that, with no luck
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: PHP - setting variable in link?

Post by dejvos »

So try:

Code: Select all

 
foreach($_GET as $key => $val){
// Some code
}
 
You will have variable names in the $key and in $val you will have a value.

Is that it?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP - setting variable in link?

Post by jackpf »

hbraekke wrote:
jackpf wrote:How odd.

What's your code? And what version of PHP are you running?
If you read my first question...
Did you read my questions?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP - setting variable in link?

Post by califdon »

peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: PHP - setting variable in link?

Post by peterjwest »

Test:
Put the statement "print_r($_GET); die();" above all of your code.

Access your site with the variable "?foo=bar"
e.g. http://www.mydomain.com/index.php?foo=bar

The page should say "Array ( [foo] => bar )"

Success:
Congratulations you have just used GET

Fail:
There is something wrong with your php/server
Post Reply