joshipoditunes Posted September 14, 2013 Share Posted September 14, 2013 (edited) DISCLAIMER: this is my first time on this site, and I am a very beginning programmer still in high school, so please bear with any mistakes I might make. (if you have ANY advice or comments, feel free to share them with me, any help is appreciated) I am basically looking to make a series of css checkboxes (I only have one right now for testing purposes), which will submit onclick and store the value in a txt file. Upon loading, it will read that text document and set the checkbox default to what it was before. I could use the PHP session method, but I would like the default to carry on through reboots, etc. Currently, the file current.txt has a 1 in it, but the checkbox is still off. When I click the checkbox, it refreshes, but then goes back to off. any advice? <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="mystyle.css"> </head> <body> <form action="index3.php" method="post"> <div class="onoffswitch"> <?php //writing to the file with the results of POST $my_filew = 'current.txt'; $handlew = fopen($my_filew, 'w'); if (isset($_POST["onoffswitch1"]) && $_POST["onoffswitch1"] == "on") { fwrite($handlew,'1'); fclose($handlew); } if (isset($_POST["onoffswitch1"]) && $_POST["onoffswitch1"] == "0ff") { echo "hello"; fwrite($handlew,'0'); fclose($handlew); } //reading from the file and setting the default $my_filer = 'current.txt'; $handler = fopen($my_filer, 'r'); $datar = fread($handler,filesize($my_filer)); fclose($handler); if ($data == 1) { $checked="checked"; } else { $checked=""; } //outputting the html $option='onclick="setTimeout(function() {submit();},1250);"'; echo '<input type="checkbox" name="onoffswitch1" class=onoffswitch-checkbox" id="myonoffswitch" '.$checked.' '.$option.'>'; echo "\n"; ?> <label class="onoffswitch-label" for="myonoffswitch"> <div class="onoffswitch-inner"> <div class="onoffswitch-active"><div class="onoffswitch-switch">ON</div></div> <div class="onoffswitch-inactive"><div class="onoffswitch-switch">OFF</div></div> </div> </label> </div> </form> </body> </html> Edited September 14, 2013 by joshipoditunes Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 14, 2013 Share Posted September 14, 2013 I'm not sure what you are trying to do with the files. Are you just trying to set the ON/OFF text on the page to "active" or "in-active"? If you post the reasons, or goal of what you are trying to accomplish, then we can give you the best course of action to take. There are several ways to do anything in PHP, but some ways are more efficient. Case in point. Instead of fopen(), fwrite(), fclose(), you could just use file_put_contents(), file(), or file_get_contents(). Quote Link to comment Share on other sites More sharing options...
joshipoditunes Posted September 14, 2013 Author Share Posted September 14, 2013 (edited) I am trying to use $checked to set the checkbox default to "checked" if the txt file has a 1 in it. as for the fopen, thank you, I will start editing Edited September 14, 2013 by joshipoditunes Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 14, 2013 Solution Share Posted September 14, 2013 $datar = fread($handler,filesize($my_filer));fclose($handler);if ($data == 1) Quote Link to comment Share on other sites More sharing options...
joshipoditunes Posted September 14, 2013 Author Share Posted September 14, 2013 Thank You both so much! @barand this error fix combined with a permissions fix Finally made it work! now for the next part: more checkboxes!!! Thanks!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.