Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.''];
        

//// check to see it player already in the availabilty list for this match
        $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;
                    }
            } 
        }
// sql to add a record
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.
Posted
Updated 16-Jan-19 2:57am

Quite often when I post a question I immediately find the answer even though I have spent hours trawling the net.
I have used another input <tag> called 'readonly' which does precisely that and still creates the $_Post variable.
 
Share this answer
 
Comments
Richard Deeming 18-Jan-19 10:54am    
NB: That won't prevent a determined user from modifying the posted data using the browser's developer tools. :)
Let me give you an alternative that is easier:   instead of using an <input> or <textarea> - why not simply display the text in, for example, a <div> ? If it's a table cell, you could just simply put text in the cell.

You would need to go out of your way to make the text changeable in these elements - and for normal use: bullet proof!
 
Share this answer
 
Comments
Dave the Golfer 20-Jan-19 10:35am    
I do not only want to display the text but I must be able to read it again later when the checkbox is checked so I can update the data tables. If I simply put text in the cell how do I read it again to update the data tables?
W Balboos, GHB 21-Jan-19 14:48pm    
Every element can be read, via the DOM, for example, as a value, or, in some cases, as the innerHTML. As for which to read - you could associate the id of the text containing area with the id of the checkbox (area1->chk1, area2->chk2). There a so many ways to do this sort of thing, depending up the circumstances. If you must use a form, then the checkbox state can set/clear a value in a hidden form element. If value not empty, save; if empty, ignore.

Use some imagination!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900