I am saving the state of a checkbox to a text file and reading it back in to show either a checked or non-checked box. The saving part is no problem - depending on whether I check or uncheck the box the value "Checked" or "Unchecked" is written to the text file. Echoing echo $lines[0];
results in either value as expected. However, when using if ($lines[0] == "Checked")
as shown in my code below, the value doesn't seem to be detected and therefore the echo that follows is not executed. What is the correct syntax for this situation?
<?php
$file = "textfile.txt";
$lines = file($file);
?>
<input type="hidden" name="data1" value="Unchecked" />
<input type="checkbox" name="data1" value="Checked" class="checkbox"
<?php
if ($lines[0] == "Checked") {
echo 'checked="checked"';
}
?>
/>
You probably have a line-break in your $lines[0]
variable.
What you could do is:
if (trim($lines[0]) == "Checked") {
But what you should do, is switch to a database.