How to track click on the button in php?

There are 3 buttons:
<div class="offer-photo">
 <input type="image" name="first" id="one_photo" src="img/white-boot.png" class="photo-boots chose" onclick="photo_one()">
 <input type="image" name="second" id="two_photo" src="img/blue-boot.png" class="photo-boots" onclick="photo_two()">
 <input type="image" name="third" id="three_photo" src="img/red-boot.png" class="photo-boots" onclick="photo_three()">
 </div>


Clicking on them should change a variable in php:
<?php
 $current = 2;
 if ($current == 1){
 echo '<img src="img/boots-galery.png" alt="" class="boot-galery">';
 echo '<div class="wrapper-offer">
 <img src="img/galery-white-1.jpg" alt="" class="offer-galery">
 <img src="img/galery-white-2.jpg" alt="" class="offer-galery">
 <img src="img/galery-white-3.jpg" alt="" class="offer-galery">
</div>';
}
 else if ($current == 2){
 echo '<img src="img/blue-boots-galery.png" alt="" class="boot-galery">';
 echo '<div class="wrapper-offer">
 <img src="img/galery-blue-1.jpg" alt="" class="offer-galery">
 <img src="img/galery-blue-2.jpg" alt="" class="offer-galery">
 <img src="img/galery-blue-3.jpg" alt="" class="offer-galery">
</div>';
}
 else if ($current == 3){
 echo '<img src="img/red-boots-galery.png" alt="" class="boot-galery">';
 echo '<div class="wrapper-offer">
 <img src="img/galery-red-1.jpg" alt="" class="offer-galery">
 <img src="img/galery-red-2.jpg" alt="" class="offer-galery">
 <img src="img/galery-red-3.jpg" alt="" class="offer-galery">
</div>';
}
 ?>

How to do it?)
March 23rd 20 at 19:09
5 answers
March 23rd 20 at 19:11
Solution
Clicking on them should change a variable in php:
First and foremost, You need to understand that variables in PHP will be reinitialized at every new start of the script and the fact that all variables and values in PHP script exist only in the moment, while the script is running.

If You need to store the variable's value between requests - you can use e.g. session or database or repository or...

How to do it?)
By sending the form to the server or through AJAX request, these are the most common methods.
I can't do this:
1.Pressing the button
2.Change the value of the variable
??? - bernadette commented on March 23rd 20 at 19:14
@bernadette, please specify exactly where You struggle or what to explain because I apparently don't fully understand Your question. Button, the default option sends to the server form (tag <form>), in which it (the button) is located. In this form, respectively. You send a variety of fields whose values later you can get the variables $_GET and $_POST. - Quinton_Raynor86 commented on March 23rd 20 at 19:17
@bernadette, you can, but between your 1 and 2 happen much more.
And before 1 (this button itself will not appear the same).
And you'll want to do something and after 2. - bernardo_Kilback92 commented on March 23rd 20 at 19:20
I can't do this:
1.Pressing the button
2.Change the value of the variable
???

Briefly - no. Welcome to the world of scripting languages.

All works like this:
1. The user drives in the address bar the url of your website
2. The server prepares a response (html). At this point peremennye are initialized, they are set values, etc.
3. The server finishes its work. All peremennye removed from memory.
4. The client receives data from the server.
5. The user sees html, server is prepared.

Subsequent actions user can initialize a new request to the server, but then ALL peremennye will inicializalasa again. - Marisa_Parisian commented on March 23rd 20 at 19:23
March 23rd 20 at 19:13
Solution
No way. PHP works on the server and sees what the customer clicks.

The browser must send a request to the server, usually POST or GET request (there are other options, but these two most often).

Open the HTML tutorial and read how to construct links and forms. Or ajax.
March 23rd 20 at 19:15
Poterci AJAX.
Look the solution will be something like this - clicking the button is an AJAX request to the server, in the data send button on which clicked, on the server process and return to the browser.
March 23rd 20 at 19:17
For each button you have a name in the name attribute with different names for the three buttons:
<input name="first"' ... >
<input name="second"' ... >
<input name="third"' ... >


On the receiving side, check that variable depending on it performs the desired block $current.
$current = $POST[name]; (or GET)
Only instead of $current==1 enter $current==first, etc.

Something like that. Have not kodil. Should work, need to check.
Your response would add that you need a form tag to add because he's not
And yet You have a mistake not $POST and $_POST please correct. - Nick_Walker commented on March 23rd 20 at 19:20
March 23rd 20 at 19:19
Seeing the lack of tag <form> and <submit> and the presence onclick="photo_three()" , you can assume that you have AJAX. In this JS function you need to call the php page from the server and "use such as session or database or repository or..." (what he said @Quinton_Raynor86 ) to save the result of depression. For example, мой.сайт.рф/photo_three.php or mine.site.RF/photo.php?current=three (so don't do it :) - this is just an example - safety to goes to hell). You need to write another page in php, the controller, Look for AJAX+PHP .
When you open the page your php script initialize the variable $current out of the vault which I chose (Session or database ... )

Find more questions by tags PHP