Skip to content

Instantly share code, notes, and snippets.

@Mshriver2
Last active January 28, 2020 05:42
Show Gist options
  • Save Mshriver2/2edd51ec66a4b9faa7d094a961d905d3 to your computer and use it in GitHub Desktop.
Save Mshriver2/2edd51ec66a4b9faa7d094a961d905d3 to your computer and use it in GitHub Desktop.
Record the order of HTML button clicks on a webpage using PHP sessions
<?php
session_start();
if(!isset($_SESSION['clickOrder'])){
$_SESSION['clickOrder'] = array();
} elseif (!isset($_SESSION['currentLevel'])) {
$_SESSION['currentLevel'] = 10;
}
// checks if any buttons are pressed
if (isset($_POST['button1'])){
bClicked('1');
$msg = "btn1";
} elseif (isset($_POST["button2"])){
bClicked('2');
$msg = "btn2";
} elseif (isset($_POST["button3"])){
bClicked('3');
$msg = "btn3";
} elseif (isset($_POST["button4"])){
bClicked('4');
$msg = "btn4";
}
// Used to record order of button presses, storing them in a session array
function bClicked($btn_value){
array_push($_SESSION['clickOrder'],$btn_value);
$clickCount = count($_SESSION['clickOrder']);
echo $_SESSION['currentLevel'];
echo "click count: " . $clickCount;
if (count($_SESSION['clickOrder']) == $_SESSION['currentLevel']){
session_unset();
session_destroy();
}
}
}
?>
<html>
<div class="firstgrid">
<form action="" method="post">
<input type="button" id="btn1" class="button1" name="button1" value="1">
<input type="button" id="btn2" class="button2" name="button2" value="2">
</div><br>
<div class="secondgrid">
<input type="button" id="btn3" class="button3" name="button3" value="3">
<input type="button" id="btn4" class="button4" name="button4" value="4">
</div>
<div class="thirdgrid">
<input type="button" id="start" class="start" name="start" value="Start Game">
</form>
</div>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment