lundi 29 octobre 2018

Write and Read Checkbox status to/of file with php

I am trying to save the status of my checkbox whether it is true or false (checked/unchecked) to a file. I managed to write the checkbox value to a file but I have no idea if this is even the right way to do it and I also don't know how to load it again. I want that my checkbox status of the last time is "remembered" by reloading the page. Using local storage isn't a option for me sadly.....

here my code:

<form action="database.php" method="post">
<input type="hidden" name="check2" value="0" /> 
<input type="checkbox" name="check2" value="1" /> 

<input type="submit"  value="senden" />
</form>



<?php

$handle = fopen("saver.json", "a");

$fh = fopen( 'saver.json', 'w' );
fclose($fh);  
  
 foreach($_POST as $value) {
  fwrite ($handle, $value);

 }
 
 fclose($handle);

?>

so this first deletes the old saved value and then writes a 1 or a 0 in the file. Am I on a good way or do I think too simple?

All help is highly apprecciated ! Thanks a lot




Aucun commentaire:

Enregistrer un commentaire