How do I change the value of a PHP variable by clicking a link?

Asked

Viewed 1,048 times

2

For example, I have a variable called:

$idEmpreendimento = "1";

I wish that by clicking on a given link, I could change this value to 2. It is possible?

--------------------------- EDIT ~ Solution

For those who want the solution, I was able to solve it in the following way:

I changed the variable to:

$idEmpreendimento = (isset($_GET["id"])) ? $_GET["id"] : "1";

And in the links, I put, for example

<a href="?id=1">Link</a>

In this specific case, I ended up pulling the id number directly from the database with a:

echo "<li><a href='?id=" . $Listagem['id'] . "#imoveis" . "'>" . $Listagem['nome'] . "</a></li>";

Anyone who has a similar question and wants to ask, feel free.

  • The problem is that you have to remember that when the person clicked on the link, PHP has already been executed and is over. What is possible is your link calling a PHP that changes a session variable, for example, so you can use this variable later in another PHP.

  • I get it... I think I’ll need Ajax to reload the content in question then... because the idea is the following: I have a number of businesses registered in the bank and a certain place to show them, but only one at a time. Today, I pull from the table only enterprise data with ID=1. When I click on the second enterprise, I need it to change that variable to 2 and reload the contents relating to enterprise 2, and so on... Any suggestions?

  • I think you can completely forget about this variable thing and already put together the right link. For example, if the user is viewing enterprise 17, the link will already be mounted as "visualize.php? emp=18". So you already kill the 2 problems at once. The link "next" and "previous" would already point to the correct enterprise to be loaded in the click.

  • I get it... and how does this thing work? I’m still a little beginner with PHP, I try to do things as it seems to make sense in my head, hehehe.

  • obviously that first you need to learn the basics of the language by doing some tests. Among the things, it is good to learn the use of $_GET to catch the id link instead of starting with a variable, and at the time of building the page, you will use something like echo '<a href="visualizar.php?id='.(id+1).'">' (but I am simplifying, there is no way to teach everything in a question of the site, give a studied in PHP, and in the examples of the pages of functions).

  • Got it... all right, thanks for the tip!

  • Edit your question, remove the answer component and answer your question with the solution to your problem (correct way to make the solution known).

Show 2 more comments

2 answers

1

A "simple" way would be, Use Ajax and $_SESSION. You create a session and each click sends a message via ajax to a page that updates the session value.

$_SESSION['contador'] += 1;

Then recover the session value also via ajax. And then switch to PHP when you need it.

1


You can use $_GET as described in the comments.

Mount the link

<a href="link-para-pagina.php?id=2">Link</a>

After the ? in the link, the variables are declared $_GET, if you want several, use & to separate them, thus: link-para-pagina.php?id=2&grupo=7.

And receive the variables:

$idEmpreendimento = (isset($_GET['id']) ? $_GET['id'] : '1');

The expression means that if the variable $_GET['id'] was declared, then the variable $idEmpreendimento will receive its value, otherwise it will receive the default value 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.