Simple PHP button click counter

Hello everyone,

I have a question regarding how to make a simple button with HTML and PHP. By clicking this button the number will be counted up every time it’s clicked.
I have been scouring the web and can either only find how to make this with a .txt file, using JavaScript/JQuery or any other programming language.

This is for an assignment and we’re not allowed to create any other files or use any other programming language.
As mentioned, only HTML and PHP are allowed, and it should be all in one file.

Besides giving the code (if possible), I would like to know why that code should be used. I’d like some explanation so I can understand it better too.

So far I’ve got this:

[php]<?php

if (isset($_GET[‘counter’])) {
$counter = $_GET[‘counter’];
}

else {
$counter = “0”;
}
?>

<!doctype html>

Counter

[/php]

I would do it like this:

[php]<?php
$counter = isset($_GET[‘counter’]) ? $_GET[‘counter’] : 0;
?>

<!doctype html>

Counter

Counter: <?= (int) $counter ?>

[/php]

[php]$counter = isset($_POST[‘counter’]) ? $_POST[‘counter’] : 0;[/php]changed to shortand if/else. makes it easier to see that one variable is set to some value based on condition.

+1 for dividing logic and view.

if you have any questions just ask

Hello JimL :slight_smile:

I decided to register now after some more thought, and I really think this website as a forum has a lot to offer.

Anyways, back to php.

The code indeed works wonders and because it is simply built, it’s also a lot less scarier than I originally thought.

However, I do have a few questions:
In the full given code you’ve written $_GET, but in the separate single PHP code line only $_POST
-Why are these different?
-And when do you use $_GET or $_POST ?

Also, could you explain to me what this part exactly does?
–> $counter = isset($_GET[‘counter’]) ? $_GET[‘counter’] : 0;
I understand counter as a variable is created, and then you need to retrieve the counter information (with the first $_GET) and at the end (the 0) is to start off the code with 0, before any clicking has occurred. But what does this next specific part do of that section?
? $_GET[‘counter’]

Thanks in advance, and thank you for your previous help :slight_smile:
It is very much appreciated.

This was a typo, I changed the code to use POST but forgot to save the modified version. This was what I ended up with. I chose to post the data as it looks cleaner as we aren’t cluttering the address bar.

[php]<?php
$counter = isset($_POST[‘counter’]) ? $_POST[‘counter’] : 0;
?>

<!doctype html>

Counter

Counter: <?= (int) $counter ?>

[/php]

Basically variables that are passed in the address bar (page.php?variable=value&variable=value) are $_GET variables. While variables passed “hidden” are $_POST variables.

It’s a shorthand if/else, it guarantees that the $counter value is set to a value, it’s the same as writing this:

[php]if (isset($_GET[‘counter’])) {
$counter = $_GET[‘counter’];
} else {
$counter = 0;
}[/php]

Ok, thank you once again for your quick reply :slight_smile:

Could you perhaps give examples when one would use $_GET in their code and when they would use $_POST?
Just so I have a better idea when I could/should use it myself.

I hope you don’t mind, but I’m curious how the entire code would be built up if you would use $_GET instead of $_POST?
Because the code I had in my first post used $_GET, but it didn’t work (I know I’m missing something, I just have no idea what exactly) ^^;

Thanks again in advance.

Post is normal for posting form data.

Get is used when you want the user to see the data
Ie
search.php?q=searchterm
news.php?id=164
Etc

My first code example use get :slight_smile:

Your code never echo the counter value, and you haven’t icluded it in your form so its not submitted/incremented.

[php]<?= $ variable ?>[/php]
Is a shorthand echo that is easy to use when echoing into html

Its the same as

[php]<?php echo $ variable ?>[/php]

Ah, allright ^^
Thank you a lot, JimL, I appreciate your help.

Sponsor our Newsletter | Privacy Policy | Terms of Service