[Solved] PHP: Update variable when link is clicked [closed]


Yes it is possible.

One thing you could do, as some people touched in the comments, is to use a GET method, which is essentially parsing a variable through a URL string.

Example:

<a href="https://stackoverflow.com/questions/52229108/example_page_a.php?id=1">I'm a link parsing a variable!</a>

Now, by clicking that link, you will see this: “https://stackoverflow.com/questions/52229108/example_page_a.php?id=1” in the URL. the part id=1 is actually a variable that you are able to fetch through the GET method like so:

<?php
$my_get_variable=$_GET['id']; // This variable will be equal to 1
?>

You can then do whatever you wish with this variable in your “example_page_a.php” file.

i.e. $my_get_variable=$my_get_variable+1;

$my_get_variable is now equal to 2.

The below link will then again parse the newly altered variable to another page using the same method, and you’d have to retrieve the variable the same way as previously shown.

<a href="https://stackoverflow.com/questions/52229108/example_page_b.php?id=<?php echo $my_get_variable; ?>">I'm a link parsing the altered variable!</a>

I.e. $my_get_variable=$_GET['id']; // This is the newly altered variable!.

Notice that you don’t necessarily have to use 2 different pages, but can keep linking to the same page, and the logic still applies.

Another way is to do it by sessions.

Example:

$_SESSION["my_variable"]=1;

you now have a session variable that you can access on any page at any time, as long as you remember to do a session_start(); before doing anything with the session variables.

So let’s say that you set $_SESSION["my_variable"]; to be equal to 1, as I did earlier, in a file called “example_page_a.php” . You will now be able to access this variable in the other file called “example_page_b.php”, and alter that session variable into anything you want.

$_SESSION["my_variable"]=$_SESSION["my_variable"]+1;. Your session variable will now be equal to 2 instead of 1, and it will carry over to any page, as long as you remember to do a session_start(); before doing anything that has to do with your sessions.

Note that you don’t have to work with numbers, or stick with one data type. You can literally make variables with numbers, overwrite them with strings, or vise versa etc. It’s completely up to you what you do with those variables.

Now, you weren’t very specific in your question about the use of it, but I trust that the answer was clear enough. You can construct your own logic around the variables, where and when they should change etc.

And on a final note. The GET method will have the variables visible in the URL, so make sure that those variables aren’t meant to be kept unseen by the user. Sessions, however, will not display anywhere, unless you choose to echo them out or anything like that, which means they are perfect for situations where you need to keep variables that should be easily accessible on any page at any time, whilst still being outside of the users perspective.

I would recommend that you read up on the different variable parsing methods, such as GET, POST and SESSION.

1

solved PHP: Update variable when link is clicked [closed]