With some help, I made a truck check form with buttons for pass, warning, and fail. As well as a comment button that adds a line for a comment. This is all being done in a while loop from mysqli_fetch_array.
How do I post the data from the filled out form to the mysql db? I dont have much experience posting to mysql using dynamic table loops.
Here is my code:
<div class="container-xxl flex-grow-1 container-p-y">
<h2 align="center"><?php echo $appname?></h2><br />
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<?php while ($checks = mysqli_fetch_array($checksqry)) {
echo '<tr>';
echo '<td style="width: 20%">'.$checks['check_name'].' <br><br>
<input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnpass[' . $checks['id'] . ']" value="Pass">
<label class="btn rounded-pill btn-outline-success" for="btnpass[' . $checks['id'] . ']">Pass</label>		
<input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnwarning[' . $checks['id'] . ']" value="Warning">
<label class="btn rounded-pill btn-outline-warning" for="btnwarning[' . $checks['id'] . ']">Warning</label>		
<input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnfail[' . $checks['id'] . ']" value="Fail">
<label class="btn rounded-pill btn-outline-danger" for="btnfail[' . $checks['id'] . ']">Fail</label>		
<button onclick="myFunction('. $checks['id'] . ')" type="button" name="comment[' . $checks['id'] . ']" id="comment[' . $checks['id'] . ']" class="btn btn-info" style="float: right">Click</button>
<br><br>
<div style="display:none;" id="commentlinediv[' . $checks['id'] . ']">
<input type="text" class="text-line" placeholder="Type Comment Here" id="commentline[' . $checks['id'] . ']"/>
</div>
</td>';
echo '<tr>';}?>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
<style>
input[type="text"] {
border:none; /* Get rid of the browser's styling */
border-bottom:1px solid black; /* Add your own border */
}
.text-line {
background-color: transparent;
color: black;
outline: none;
outline-style: none;
border-top: none;
border-left: none;
border-right: none;
border-bottom: solid #eeeeee 1px;
padding: 3px 10px;
width: 300px;
}
</style>
<script>
function myFunction(var1) {
document.getElementById("commentlinediv[" + var1 + "]").style.display = "inline";
}
</script>
2
Answers
Firstly, let’s fix a few things in your original code
You need to specify the action in your form tag, so change say to
you also need to change your submit button type from button to submit, so change say to
You will need to specify a name (array type, such as
xcomment[]
) in the submission, data part, so change toIn order to pass the IDs (array type,
xid[]
) to the target PHP, add the following below the while lineNow, in the PHP (update.php), use the following:
(assuming that the db table name is table_name and the comment field is commentline)