捕获动态表中HTML元素的值 [英] Capture values of HTML elements in a dynamic table

查看:65
本文介绍了捕获动态表中HTML元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <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值来使该名称唯一.

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

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

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

编辑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');

}


?> 

您的出局将是

此处是您的唯一ID,可帮助您将数据更新到数据库中

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

您可以使用print_r($ _ POST);进行检查.在同一页面上

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

I have the following dynamic table generated from database.

<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>

What I need

Lets say user clicked Save button of a particular row of that table, I want the respective values of that row to be saved in database using php $_POST or $_GET method something like the following

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

Issue

My issue is, I don't know how to get the $_POST values of the form since name of every items are dynamically generating. I tried to make the name unique to each element by adding the unique $id value to it as per the following.

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

I can't capture this unique name values by the following method. Does anyone know why?

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

Edit 1

To get a better picture, following is the screenshot of a row with the button

解决方案

Correct Me If I am not wrong.. Here you want to get the all value from a form Right? on click of button submit? Why you are making this button href? Suppose we have 3 data into my $result and loops look like below

 <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');

}


?> 

Your Out put will be

here sourece is your unique Id which helps you to update the data into your DB

After submitting the form from any tr(row) you will redirect to same page with POST method and get data for that particular row on that page so you can easily update it as you want

You can check this by using print_r($_POST); on same page

If you have any questions please feel free to ask me...

这篇关于捕获动态表中HTML元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆