I'm trying to use checkboxes to update information in a MySQL database.
The 'Sent' column is a Boolean.
Is there a way to put a checkbox inside my php code,tbody so, instead of <td>".$infoItems['Sent']."</td>
there's a checkbox instead of zeroes.
<table class="scroll">
<thead>
<tr>
<th>Schools Name</th>
<th>Schools Email</th>
<th>Sent</th>
</tr>
</thead>
<tbody>
<?php
$execItems = $conn->query("SELECT Name,SchoolMail,Sent FROM Schools");
while($infoItems = $execItems->fetch_array())
{
echo "
<tr>
<td>".$infoItems['Name']."</td>
<td>".$infoItems['SchoolMail']."</td>
<td>".$infoItems['Sent']."</td>
/* ^Insert a checkbox here instead of this ^ */
</tr>
";
}
?>
</tbody>
</table>
</body>
</html>
You could try something like this
echo '<tr>
<td>'.$infoItems['Name'].'</td>
<td>'.$infoItems['SchoolMail'].'</td>
<td>
<input type="checkbox"'.($infoItems['Sent']?' checked':'').'
data-mail="'.$infoItems['SchoolMail'].'"
data-name="'.$infoItems['Name'].'"
onchange="ajax_call_function(this)"
/>
</td>
</tr>' ;
It will call a javascript function :
<script type="text/javascript">
function ajax_call_function(element) {
console.log(element.getAttribute("data-name"));
console.log(element.getAttribute("data-mail"));
// make your AJAX call to a server-side script
// that updates your database with these informations.
//
// OR
//
// Update some hidden fields in a form and submit it.
}
</script>