Checkbox Toggle

Ask about a PHP problem here.
Post Reply
azmal101
Posts: 5
Joined: Wed Mar 27, 2013 3:32 pm

Checkbox Toggle

Post by azmal101 »

[syntax=php]

<?php
$con=mysqli_connect("localhost","root","","User_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Assessments");

echo "<table class='sortable' border='0' cellpadding='40'
cellspacing='0'>
<thead>
<tr>
<th>Title</th>
<th>Lecturer</th>
<th>Start</th>
<th>Due</th>
<th>Completed</th>
</tr>
</thead>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Lecturer'] . "</td>";
echo "<td>" . $row['Start'] . "</td>";
echo "<td id='Due'>" . $row['Due'] . "</td>";
echo "<td>" ."<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' />";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>
[/syntax]

Hello guys I was wondering if the checkbox would get check when the date on $row['Due'] is reached for example 14/04/2013 is the date on Srow['Due']. The checkbox should automatically get the date and check the checkbox permanently.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Checkbox Toggle

Post by ExtremeGaming »

You're going to need to use the php date function.

[syntax=php]<?php
If($row['date'] == date(stuff)) {
// use the checked property of the checkbox
} else {
// use a normal checkbox
}
?>[/syntax]
<?php while(!$succeed = try()); ?>
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Checkbox Toggle

Post by Helx »

If it's the HTML bit you're unsure of, the following will check the box by default (use it alongside ExtremeGaming's suggestion):

[syntax=xhtml]<input type="checkbox" name="travelMethod" value="bike" checked>I use a Bike[/syntax]
And if you don't want the user to be able to un-check the box:

[syntax=xhtml]<input type="checkbox" name="travelMethod" value="bike" checked disabled>I use a Bike [/syntax]
However, do not rely on the "disabled" part of the input.
This can be modified client-side to be a normal editable checkbox.
Post Reply