I have populated a table on an HTML WEB page from a MySQL table using a loop. At the end of each row I have included a checkbox. The data from the table is store in the table cells using a
input type="text" box. I do not want the user to change any of the table data displayed in the text boxes but I do want them to check some of the check boxes.
Code for creating the table is as follows;
$x = 0;
while ( $db_field = mysqli_fetch_assoc($result) ) {
$_SESSION["Opponent"] = $db_field['Opponents'];
$uOpponentName = $_SESSION["Opponent"];
$_SESSION["Venue"] = $db_field['Venue'];
$uVenue = $_SESSION["Venue"];
$originalDate = $db_field['Date'];
$_SESSION["Date"] = date("d-m-Y", strtotime($originalDate));
$uPlayer = $_SESSION["Player"];
$SQL = "SELECT * FROM ".DB_AVAIL." WHERE opponents = '$uOpponentName' AND venue = '$uVenue' AND player_name = '$uPlayer'";
$result2 = mysqli_query($db_handle, $SQL);
$x=$x + 1;
if ($result2->num_rows >0) {
echo'<tr>
<td style="width: 150px;"><input type="text" name="oppon'.$x.'" size="20" value="'.$_SESSION["Opponent"].'" ></input></td>
<td style="width: 37px;"><input type="text" name="ven'.$x.'" size="5" value="'.$_SESSION["Venue"].'" ></input></td>
<td style="width: 80px;"><input type="text" name="dat'.$x.'" size="10" value="'.$_SESSION["Date"].'" ></input></td>
<td style="width: 100px;" align = "center"><input type="checkbox" name="avail'.$x.'" value="avail" checked="checked"></td>
</tr> ';
}else{
echo'<tr>
<td style="width: 150px;"><input type="text" name="oppon'.$x.'" size="20" value="'.$_SESSION["Opponent"].'" ></input></td>
<td style="width: 37px;"><input type="text" name="ven'.$x.'" size="5" value="'.$_SESSION["Venue"].'" ></input></td>
<td style="width: 80px;"><input type="text" name="dat'.$x.'" size="10" value="'.$_SESSION["Date"].'" ></input></td>
<td style="width: 100px;" align = "center"><input type="checkbox" name="avail'.$x.'" value="avail"></td>
</tr> ';
}}} ?>
To save the data back into another table based on whether the checkbox has been checked I used the following code;
$y = $_SESSION["RowCount"];
for ($x = 1; $x <= $y; $x++) {
$Already = false;
if (isset($_POST['avail'.$x.''])) {
$uPlayerName = $_SESSION["Player"];
$uOpponent = $_POST['oppon'.$x.''];
$uVenue = $_POST['ven'.$x.''];
$SQL = "SELECT `player_name` FROM ".DB_AVAIL." WHERE `opponents`= '$uOpponent' AND `venue`= '$uVenue'";
$result = $conn->query($SQL);
if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$PlayerName = $row['player_name'];
If ($uPlayerName==$PlayerName ){
$Already = true;
Break;
}
}
}
If ($Already == false){
$sql = "INSERT INTO ".DB_AVAIL." (`player_name`, `opponents`, `venue`) VALUES ('$uPlayerName','$uOpponent','$uVenue')";
if ($conn->query($sql) === FALSE) {
print "Error adding record: " . $sql . "<br>" . $conn->error;
}
}
All this works okay but the Text boxes are open to editing, which I do not want.
Question is, how can I display the table data on a HTML form so that it cannot be edited but can still be passed back to the server in PHP to be processed and saved as necessary?
What I have tried:
I tried disabling the Text boxes by change the code as follows
<td style="width: 150px;"><input type="text" name="oppon'.$x.'" size="20" value="'.$_SESSION["Opponent"].'" ></input></td>
to the
<td style="width: 150px;"><input type="text" name="oppon'.$x.'" size="20" value="'.$_SESSION["Opponent"].'" disable></input></td>
however this failed as the
$_POST['oppon'.$x.''] variable was not generated by the HTML page.