I'm new into coding and programming world. I'm currently attending school which mostly teach about web development.
I was trying to record how many times the user clicks on to few buttons only with PHP sessions and I'm trying to keep all the code in single page.
It does work so far, but the only problem I have right now is that whenever I refresh the page, the counter of the last button I clicked raises up. And also when I click on the button with session_destroy();
function, it doesn't reset the session right away.
Is it actually possible with PHP? Or do I really need a database?
Code:
<?php
session_start();
$clickGreen = 0;
$clickRed = 0;
if(!isset($_SESSION['clickGreen']) && !isset($_SESSION['clickRed'])){
$_SESSION['clickGreen'] = $clickGreen;
$_SESSION['clickRed'] = $clickRed;
}
if(isset($_GET["SubmitGreen"]) && !empty($_GET['SubmitGreen'])){
$_SESSION['clickGreen']++;
}
if(isset($_GET["SubmitRed"]) && !empty($_GET['SubmitRed']) ){
$_SESSION['clickRed']++;
}
if(isset($_GET["SubmitRefresh"])){
session_destroy();
}
$g = $_SESSION['clickGreen'];
$r = $_SESSION['clickRed'];
?>
<html>
<head>
<style>
</style>
</head>
<body>
Cicks on green = <?php echo $g ?><br/>
Clicks on red = <?php echo $r ?><br/>
<form name="form1" method="get" action="index.php">
<input type="submit" name="SubmitGreen" value="Green" />
<input type="submit" name="SubmitRed" value="Red" />
<input type="submit" name="SubmitRefresh" value="Refresh" />
</form>
</body>
</html>