Page 1 of 1
Clicking a link and setting a variable
Posted: Tue Mar 07, 2006 8:08 pm
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?
Posted: Tue Mar 07, 2006 8:18 pm
by elecktricity
Code: Select all
<?PHP
echo '<a href="http://link.com" ' . $variable . '="1">';
?>
Posted: Tue Mar 07, 2006 8:23 pm
by Citizen
Thanks!
Posted: Tue Mar 07, 2006 8:25 pm
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">
Posted: Tue Mar 07, 2006 8:37 pm
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?
Posted: Tue Mar 07, 2006 10:22 pm
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
?>