Jump to content

HTML/PHP help needed

Go to solution Solved by Barand,

Recommended Posts

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>
<link rel="stylesheet" type="text/css" href="mystyle.css">

<form action="index3.php" method="post">
<div class="onoffswitch">

//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")
if (isset($_POST["onoffswitch1"]) && $_POST["onoffswitch1"] == "0ff")
echo "hello";

//reading from the file and setting the default
$my_filer = 'current.txt';
$handler = fopen($my_filer, 'r');
$datar = fread($handler,filesize($my_filer));
if ($data == 1)

//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>

Edited by joshipoditunes
Link to comment
Share on other sites

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().

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.