Created
May 16, 2020 19:08
-
-
Save jackcrane/f4e14f5e0abe3eb1b66de26174a5840e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$switchstate = ""; // Init a variable to keep track of the switched state of the HTML input, this way we can show the state in the actual button. | |
if($_SERVER["REQUEST_METHOD"]==="POST") { // Look to see if we submitted a form to this page. If we did, then we will execute the code to change the file. | |
$file = fopen("textfile.txt", "w"); // Open the file called "textfile.txt" in "write" mode. | |
if(@$_POST["updateStatus"]==="on") { // Check the status of the input. Notice in the html at the bottom of the file the name of the checkbox input is the same as within the brackets. (updateStatus) | |
// The checkbox is checked. | |
$switchstate = "checked"; // Change the variable that holds tge state of the HTML switch so it showes checked and the user can uncheck it. | |
fwrite($file,"1"); // Write "1" to the text file, overwriting whatever was in it. | |
} else { | |
// The checkbox is unchecked. | |
$switchstate = ""; // Make sure the HTML switch gets unchecked. | |
fwrite($file,"0"); // Writes "0" to the text file, overwriting whatever was in it. | |
} | |
} | |
$status = file_get_contents("textfile.txt"); // Get the contents of a file called "textfile.txt" saved in the same directory, and save it a variable $status | |
if($status == 0) { | |
// The switch is off | |
echo "The switch is off"; // Echo: PHP's version of knowing what to show on the page. | |
} else { | |
// The switch is on | |
echo "The switch is on"; // Echo: PHP's version of knowing what to show on the page. | |
} | |
?> | |
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <!-- HTML form who's action is this file, and method is POST. --> | |
<input type="checkbox" name="updateStatus" <?php echo $switchstate;?>> <!-- Checkbox. Notice we are echoing (displaying) the switchstate computed above --> | |
<input type="submit" value="update"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment