<?php
include "config.php";
?>
<!doctype html>
<html>
<head>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['lang'])) {
$lang = implode(",",$_POST['lang']);
// Insert and Update record
$checkEntries = mysqli_query($con,"SELECT * FROM languages");
if(mysqli_num_rows($checkEntries) == 0){
mysqli_query($con,"INSERT INTO languages(checkbox) VALUES('".$lang."')");
}else{
mysqli_query($con,"UPDATE languages SET checkbox='".$lang."' ");
}
}
}
?>
</head>
<body>
<form method="post" action="">
<span>Select languages</span><br/>
<?php
$checked_arr = array();
// Create checkboxes
$languages_arr = array("Nearmisscase","FirstAidCase","LostTimeInjury","Fatal","Fire","Emission/Discharge/Spill/Leak(Abnormal)","Property Damage","HIPO");
foreach($languages_arr as $checkbox){
$checked = "";
if(in_array($checkbox,$checked_arr)){
$checked = "checked";
}
echo '<td><input type=checkbox name=lang[] value="'.$checkbox.'" '.$checked.' > '.$checkbox.'</td>';
}
?>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
// Fetch checked values
$fetchLang = mysqli_query($con,"SELECT * FROM languages");
if(mysqli_num_rows($fetchLang) > 0){
$result = mysqli_fetch_assoc($fetchLang);
$checked_arr = explode(",",$result['checkbox']);
}
// Create checkboxes
$languages_arr = array("Nearmisscase","FirstAidCase","LostTimeInjury","Fatal","Fire","Emission/Discharge/Spill/Leak(Abnormal)","Property Damage","HIPO");
foreach($languages_arr as $checkbox){
$checked = "";
if(in_array($checkbox,$checked_arr)){
$checked = "checked";
}
echo '
<td><input disabled type=checkbox name=lang[] value="'.$checkbox.'" '.$checked.' > '.$checkbox.'</td>
';
}
?>
Now i want to display the checked checkbox inside this table?
<table>
<tr>
<th>Nearmisscase</th>
<th>First Aid Case</th>
<th>Lost Time Injury</th>
<th>Fatal</th>
<th>Fire</th>
<th>Emission/Discharge/Spill/Leak(Abnormal)</th>
<th>Property Damage</th>
<th>HIPO</th>
</tr>
<tr>
<td><input type="checkbox" value="Nearmiss case" name="checkbox"></td>
<td><input type="checkbox" value="First Aid Case" name="checkbox"></td>
<td><input type="checkbox" value="Lost Time Injury" name="checkbox"></td>
<td><input type="checkbox" value="Fatal" name="checkbox"></td>
<td><input type="checkbox" value="Fire" name="checkbox"></td>
<td><input type="checkbox" value="Emission/Discharge/Spill/Leak(Abnormal)" name="checkbox"></td>
<td><input type="checkbox" value="Property Damage" name="checkbox"></td>
<td><input type="checkbox" value="HIPO" name="checkbox"></td>
</tr>
</table>
<style>
table {
font-family: sans-serif;
border-collapse: collapse;
width: 75.5%;
font-size: 15px;
}
td,
th {
border: 1px solid black;
text-align: center;
padding: 2px;
font-weight: normal;
}
</style>