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>
<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 by joshipoditunes
Link to comment
https://forums.phpfreaks.com/topic/282153-htmlphp-help-needed/
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
https://forums.phpfreaks.com/topic/282153-htmlphp-help-needed/#findComment-1449472
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.