skip to Main Content

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.

Example of my form

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>&Tab;&Tab;

           <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>&Tab;&Tab;

           <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>&Tab;&Tab;

           <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


  1. Based on your description I modify small parts of your code in order to handle to posted data from your form and then do your sql action(mean insert, update or delete).
    First you have to add this php condition at first of your code and then modify the <form> html tag to the written code.
    
    >   <?php
        if(isset($_POST['submit'])){
        // Here you can get your posted data
        // Then you have to write a query to insert, update or whatever action you need to do with your data
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <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" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
    
                    <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>&Tab;&Tab;
                 
                            <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>&Tab;&Tab;
                 
                            <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>&Tab;&Tab;
                 
                            <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="submit" 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>
            </body>
        </html>
    
    Hope this code helps you to solve your problem.
    Good Luck!
    
    Login or Signup to reply.
  2. 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

     <form name="add_name" id="add_name" method=POST action=update.php>
    

    you also need to change your submit button type from button to submit, so change say to

    <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
    

    You will need to specify a name (array type, such as xcomment[]) in the submission, data part, so change to

    <input type="text" name=xcomment[] class="text-line" placeholder="Type Comment Here" id="commentline[' . $checks['id'] . ']"/>
    

    In order to pass the IDs (array type, xid[]) to the target PHP, add the following below the while line

     echo '<input type=hidden name=xid[] value='. $checks['id']. '>';
    

    Now, in the PHP (update.php), use the following:
    (assuming that the db table name is table_name and the comment field is commentline)

    <?php
    include "db.php"; // db connection
    
    foreach ($_POST["xid"] as $index => $id) {
      $sql = "UPDATE table_name SET commentline=? WHERE id=?";
      $stmt= $conn->prepare($sql);
      $stmt->bind_param("si", $_POST["xcomment"][$index], $id);
      $stmt->execute();
    
    }
    
    ?>
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search