I want to load a value (0/1) from a mysql database and then change a checkbox to "checked" according to the value obtained. I had a look at DOMElement but I get a blank page when adding the code. Here is the code I tried:
$html = '<input type="checkbox" name="alarm" value="1">';
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('input') as $item)
{
$item->setAttribute('checked', 'true');
echo $dom->saveHTML();
exit;
}
To be quite frank this means nothing to me but it looks as if it should work.. The setAttribute
take $name
and $value
as paramaters, but a checkbox has only a attribute checked
so it got me in a flat spin abit. Here is all my code. I'm open for any useful criticism, as I am a noob and still learning.. :)
PHP
$sqlSelect = "SELECT value FROM elements WHERE element='Alarm'";
$result = $conn->query($sqlSelect);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "Value: " . $row["value"]. "<br>";
if ($row["value"]) === 1)
{
$html = '<input type="checkbox" name="alarm" value="1">';
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('input') as $item)
{
$item->setAttribute('', 'checked');
echo $dom->saveHTML();
exit;
}
}
}
}
else
{
echo "0 results";
}
if(isset($_GET['alarm']))
$alarm = 1;
else
$alarm = 0;
$sqlUpdate = "UPDATE elements SET value=" .$alarm. " WHERE element='alarm'";
if ($conn->query($sqlUpdate) === TRUE)
{
echo "Record updated successfully";
}
else
{
echo "Error updating record: " . $conn->error;
}
$conn->close();
HTML
<form id="alarm" action="test.php" method="get">
<input type="checkbox" name="alarm" value="1" checked>Alarm
</form>
Please help this dumb fool...
Aucun commentaire:
Enregistrer un commentaire