【问题标题】:How to submit table/form with grouping/id to retrieve later如何提交带有分组/ID的表格/表单以便稍后检索
【发布时间】:2014-09-13 07:54:51
【问题描述】:

我正在尝试为用户可以填写的表单编写 html5 和 PHP。当他们点击提交时,我希望有一个整体评估类别,每 5 列分组,我也可以检索。例如:

<parameters>
  <parameterID>9214</parameterID>
  <parameter>MC Bands</parameter>
  <Yevaluation/>
  <Mevaluation/>
  <Cevaluation/>
  <Kevaluation/>
  <comments/>
</parameters>
<parameters>
  <parameterID>9245</parameterID>
  <parameter>MC Streaks</parameter>
  <Yevaluation/>
  <Mevaluation/>
  <Cevaluation/>
  <Kevaluation/>
  <comments/>
</parameters>

我无法将其输入到表单和输入表中,因此我可以稍后将其检索为类别 ID。我本来打算找一个隐藏的单元格,但它会将值粘贴在第一个表格单元格中。

这是在 table.php 中:

<form  method="get" action="visEupload.php">
<table id="bigTable" border="1">
    <thead>
     <tr>
     <th id="bandY" class="col3">Bands @263mm Y</th>
     <th id="bandM" class="col3">Bands @263mm M</th>
     <th id="bandC" class="col3">Bands @263mm C</th>
     <th id="bandK" class="col3">Bands @263mm K</th>
     <th id="Comments" class="col3">Comments</th>
     </tr>
    </thead>
    <tbody>
        <tr>
            <td><input name="MCBands" value="9214" id="MCBands" visibility=hidden> <!--this isn't showing up as hidden-->
            <td><input name="Yevaluation" ></td>  //Row 0 Column 1
            <td><input name="Mevaluation" ></td>  //Row 0 Column 2
            <td><input name="Cevaluation" ></td>  //Row 0 Column 3
            <td><input name="Kevaluation" ></td>  //Row 0 Column 4
            <td><input name="comment" ></td>  //Row 0 Column 4
            <!--the above rows will repeat with different id's/names/values/cells, ex. streaks and will be really wide-->
        </tr>
    </tbody>

</table>
  <input id="submit" type="submit" class="list" name="submit" value="Submit To Database" >  

</form>

我很难找到与表单一起提交的表格数据的良好示例。我看到了这个,但它不一样 html tables

点击提交后,我在 visEupload.php 中像这样检索它,但考虑到我每隔几行添加的额外参数 ID,也许有更好的方法:

if (isset($_GET['submit'])){
        $Yevaluation= $_GET['Yevaluation'];
        $Mevaluation= $_GET['Mevaluation'];
        $Cevaluation= $_GET['Cevaluation'];
        $Kevaluation= $_GET['Kevaluation'];
        $MCBands= $_GET['MCBands'];
        $comment=$_GET['comment'];

        echo "here:".$Yevaluation.$Mevaluation.$Cevaluation.$Kevaluation.$MCBands.$comment;

        echo "here1";

        echo ("visE upload requested");

    } //submit is set

【问题讨论】:

  • 您可以通过使用类似数组的元素名称来做到这一点。请参阅我的回答 here 了解更多信息

标签: php html forms html-table http-get


【解决方案1】:

您可以在输入元素中使用类似数组的名称,例如:

<table id="bigTable" border="1">
    <thead>
     <tr>
     <th id="bandY" class="col3">Bands @263mm Y</th>
     <th id="bandM" class="col3">Bands @263mm M</th>
     <th id="bandC" class="col3">Bands @263mm C</th>
     <th id="bandK" class="col3">Bands @263mm K</th>
     <th id="Comments" class="col3">Comments</th>
     </tr>
    </thead>
    <tbody>
        <tr>
            <td><input name="MCBands[]" value="9214" id="MCBands" type="hidden"> 
            <td><input name="Yevaluation[]" value=""></td>  //Row 0 Column 1
            <td><input name="Mevaluation[]" value=""></td>  //Row 0 Column 2
            <td><input name="Cevaluation[]" value=""></td>  //Row 0 Column 3
            <td><input name="Kevaluation[]" value=""></td>  //Row 0 Column 4
            <td><input name="comment[]" value=""></td>  //Row 0 Column 4
        </tr>
        <tr>
            <td><input name="MCBands[]" value="9215" id="MCBands" type="hidden">
            <td><input name="Yevaluation[]" value=""></td>  //Row 0 Column 1
            <td><input name="Mevaluation[]" value=""></td>  //Row 0 Column 2
            <td><input name="Cevaluation[]" value=""></td>  //Row 0 Column 3
            <td><input name="Kevaluation[]" value=""></td>  //Row 0 Column 4
            <td><input name="comment[]" value=""></td>  //Row 0 Column 4
        </tr>
        <tr>
            <td><input name="MCBands[]" value="9214" id="MCBands" type="hidden">
            <td><input name="Yevaluation[]" value=""></td>  //Row 0 Column 1
            <td><input name="Mevaluation[]" value=""></td>  //Row 0 Column 2
            <td><input name="Cevaluation[]" value=""></td>  //Row 0 Column 3
            <td><input name="Kevaluation[]" value=""></td>  //Row 0 Column 4
            <td><input name="comment[]" value=""></td>  //Row 0 Column 4
        </tr>
    </tbody>

</table>

在后端,您可以使用:

if (isset($_GET['submit'])){
    $arr = array();   
    foreach($_POST["MCBands"] as $key => $val) {
        $arr[] = array(
            "MCBands" => $_POST["MCBands"][$key],
            "Yevaluation" => $_POST["Yevaluation"][$key],
            "Mevaluation" => $_POST["Mevaluation"][$key],
            "Cevaluation" => $_POST["Cevaluation"][$key],
            "Kevaluation" => $_POST["Kevaluation"][$key],
            "comment" => $_POST["comment"][$key]
        );    //semicolon added here ~M
    }

} 

【讨论】:

  • 那么如果我想从数组中删除值来打印我该怎么做呢?这似乎不起作用(我也尝试在 Yevaluation 等周围使用 ""): echo "here:".$arr[Yevaluation].$arr[Mevaluation].$arr[Cevaluation].$arr[Kevaluation]. $arr[评论];
猜你喜欢
  • 2022-01-21
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多