Clicking a link and setting a variable

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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Clicking a link and setting a variable

Post by Citizen »

Is there any way to to do something like this?

<a href="http://link.com" $variable="1";>

So that when someone clicks a link, it registers a variable?
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post by elecktricity »

Code: Select all

<?PHP
echo '<a href="http://link.com" ' . $variable . '="1">'; 
?>
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

eleckricity wrote:

Code: Select all

<?PHP
echo '<a href="http://link.com" ' . $variable . '="1">';
?>
The way you have it will echo out the value for variable in place of $variable. I think the OP wanted to be able to set a variable value when a link is clicked. To do that append the variable name and a value to the queary string of the URL you are linking to..

Code: Select all

<a href="somelink.php?variable_name=variable_value">
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Thanks, Everah, that was what I was looking for.

Is the variable set when they click the link or will I have to do something with it when the linked page loads?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here's your link...

Code: Select all

<a href="somelink.php?variable_name=variable_value">
To get the variable from that link, on the linked page, you will need to use some PHP code...

Code: Select all

<?php
// Check to see if there was a variable named 'variable_name' in the query string
if ( isset($_GET['variable_name']) ) {
    // There was, so set the value of $var_name to the passed value of 'variable_name' 
    $var_name = $_GET['variable_name'];
}

echo $var_name; // Display what was passed in the 'variable_name' var
?>
Post Reply