【问题标题】:Capture values of HTML elements in a dynamic table在动态表中捕获 HTML 元素的值
【发布时间】:2019-12-24 03:54:14
【问题描述】:

我有以下从数据库生成的动态表。

<form method="post">
    <?php
    while($result = mysqli_fetch_array($tableQueryExecute)){

        $shift1Oa = $result['operator1'];
        $shift2Oa = $result['operator2'];
        $shift3Oa = $result['operator3'];
        $shift4Oa = $result['operator4'];
        $id = $result['srNumber'];

        echo '<td scope="row">'.$id.'</td>
              <td>                         
                    <input type="number" class="form-control" id="shift1PinCount" name="shift1PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 1" min=0>
              </td>

              <td>
                    <input type="text" class="form-control" id="shift1Oa" name="shift1Oa" value="'.$shift1Oa.'" disabled>
              </td>

              <td>
                    <input type="number" class="form-control" id="shift2PinCount" name="shift2PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 2" min=0>
              </td>

              <td>
                    <input type="text" class="form-control" id="shift2Oa" name="shift2Oa" value="'.$shift2Oa.'" disabled>
              </td>

              <td>
                    <input type="number" class="form-control" id="shift3PinCount" name="shift3PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 3" min=0>
              </td>

              <td>
                    <input type="text" class="form-control" id="shift3Oa" name="shift3Oa" value="'.$shift3Oa.'" disabled>
              </td>

              <td>
                    <input type="number" class="form-control" id="shift4PinCount" name="shift4PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 4" min=0>
              </td>

              <td>
                    <input type="text" class="form-control" id="shift4Oa" name="shift4Oa" value="'.$shift4Oa.'" disabled>
              </td>

              <td><input class="form-control" type="date" id="shiftDate" name="shiftDate" value="'.date("Y-m-d").'"></td>

              <td>
                <a href="supervisorEdit.php?source='.$id.'" type="submit" class="btn btn-primary" name="savePcData" id="savePcData" value="savePcData">Save</a>
              </td>';

    }

</form>

我需要什么

假设用户单击了该表特定行的Save 按钮,我希望使用php $_POST$_GET 方法将该行的相应值保存在数据库中,如下所示

if(isset($_GET['source'])){
    $sourceId = $_GET['source'];
}

问题

我的问题是,我不知道如何获取表单的 $_POST 值,因为每个项目的名称都是动态生成的。我尝试通过按照以下方式向每个元素添加唯一的 $id 值来使每个元素的名称唯一。

&lt;input type="text" class="form-control" id="shift1Oa" name="shift1Oa'.$id.'" value="'.$shift1Oa.'" disabled&gt;

我无法通过以下方法捕获此唯一名称值。有谁知道为什么?

if(isset($_GET['source'])){
    $sourceId = $_GET['source'];
    echo $_GET['shift1Oa'.$sourceId];
}

编辑 1

为了获得更好的画面,下面是带有按钮的一行的截图

【问题讨论】:

    标签: php html mysql post html-table


    【解决方案1】:

    如果我没记错,请纠正我.. 在这里您想从表单中获取所有值对吗?点击按钮提交? 你为什么要制作这个按钮href? 假设我们的 $result 中有 3 个数据,循环如下所示

     <form method="post" action="">
    <tr>
           <td scope="row"></td>
           <input type ="text" name="sourece" value=1 />
                  <td>                         
                        <input type="number" class="form-control" id="shift1PinCount" name="shift1PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 1" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift1Oa" name="shift1Oa" value="1" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift2PinCount" name="shift2PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 2" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift2Oa" name="shift2Oa" value="2" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift3PinCount" name="shift3PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 3" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift3Oa" name="shift3Oa" value="3" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift4PinCount" name="shift4PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 4" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift4Oa" name="shift4Oa" value="4" disabled>
                  </td>
    
                  <td><input class="form-control" type="date" id="shiftDate" name="shiftDate" value="<?=date("Y-m-d")?>"></td>
    
                  <td>
                    <input type="submit" class="btn btn-primary" name="savePcData" id="savePcData" value="savePcData">
                  </td> 
    </form>
    </tr>
    <form method="post" action="">
    <tr>
           <td scope="row"></td>
           <input type ="text" name="sourece" value=2 />
                  <td>                         
                        <input type="number" class="form-control" id="shift1PinCount" name="shift1PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 1" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift1Oa" name="shift1Oa" value="11" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift2PinCount" name="shift2PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 2" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift2Oa" name="shift2Oa" value="22" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift3PinCount" name="shift3PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 3" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift3Oa" name="shift3Oa" value="33" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift4PinCount" name="shift4PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 4" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift4Oa" name="shift4Oa" value="44" disabled>
                  </td>
    
                  <td><input class="form-control" type="date" id="shiftDate" name="shiftDate" value="<?=date("Y-m-d")?>"></td>
    
                  <td>
                    <input type="submit" class="btn btn-primary" name="savePcData" id="savePcData" value="savePcData">
                  </td> 
    </form>
    </tr>
    <form method="post" action="">
    <tr>
           <td scope="row"></td>
           <input type ="text" name="sourece" value=3 />
                  <td>                         
                        <input type="number" class="form-control" id="shift1PinCount" name="shift1PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 1" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift1Oa" name="shift1Oa" value="111" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift2PinCount" name="shift2PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 2" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift2Oa" name="shift2Oa" value="222" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift3PinCount" name="shift3PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 3" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift3Oa" name="shift3Oa" value="333" disabled>
                  </td>
    
                  <td>
                        <input type="number" class="form-control" id="shift4PinCount" name="shift4PinCount" placeholder="Pin Count" title="Please key in Pin Count for Shift 4" min=0>
                  </td>
    
                  <td>
                        <input type="text" class="form-control" id="shift4Oa" name="shift4Oa" value="444" disabled>
                  </td>
    
                  <td><input class="form-control" type="date" id="shiftDate" name="shiftDate" value="<?=date("Y-m-d")?>"></td>
    
                  <td>
                    <input type="submit" class="btn btn-primary" name="savePcData" id="savePcData" value="savePcData">
                  </td> 
    </form>
    </tr>
    
    <?php
    
    if(isset($_POST['savePcData'])){
    
        echo "<pre>";
        print_r($_POST);
        die('mms');
    
    }
    
    
    ?> 
    

    您的输出将是

    这里 source 是您的唯一 ID,可帮助您将数据更新到数据库中

    从任何 tr(row) 提交表单后,您将使用 POST 方法重定向到同一页面并获取该页面上该特定行的数据,以便您可以轻松地根据需要进行更新

    您可以使用 print_r($_POST);在同一页面上

    如果您有任何问题,请随时问我...

    【讨论】:

    • 这不是我想要的。在该表中,每一行都有一个保存按钮。并且当每个按钮按下时,只有特定的行值应该被捕获并发送到 MySQL。这就是为什么我在 url 中使用 $_GET 变量并捕获序列号
    • 好的,没问题,但我看到你在输入中使用了禁用更多问题所以你不能修改数据吗?
    • 在同一行中有禁用和启用的输入。因此,所有这些字段都应该在该特定行中捕获
    • 你为什么不为每个 tr 使用多个表单我已经编辑了我的答案请检查并告诉我它是否适合你?
    • 我已经尝试过了,它可以按你的意愿工作所以我在这里粘贴我的代码,请看看这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2015-06-21
    • 2018-05-21
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多