通过表单(post)提交HTML表格数据

9

我希望在单击提交按钮时发布此HTML表格。

01- 我希望在PHP代码(服务器端)中检索此表格的数据。 02- 我还希望在另一页上显示此表格。

我正在使用

PHP,JQuery

我有一个包含许多行的HTML表格在表单标记中。

<form id="frm_test" class="form-vertical" method="post">
  <table id="mytable">
    <tr>
      <td>
        <input type="text"  name="color_1" value="" />
      </td>
      <td>
        <input type="text"  name="color_2" value="" />
      </td>
      <td>
        <input type="text"  name="color_3" value="" />
      </td>
      <td>
        <input type="text"  name="color_4" value="" />
      </td>
    </tr>
    ......
    ......
    ......
    ......
    .....
  </table>

  <input type="submit"  name="submit" value="submit" />
</form>

===========

每行有4个输入字段(每行4个单元格),表格中有数百行。如果我通过php代码按名称获取值,那么我必须编写大量代码来获取100(行)* 4(输入字段)= 400个输入。所以我的问题是“如何最好地实现这一点”。


1
您可以使用input type='hidden'从表格中提交数据。对于要发送到服务器的每个值,您都必须具有特定名称的输入。 - Alexandru Chichinete
你目前尝试了什么? - Kartikeya Khosla
我已经更新了问题。 - Ali
4个回答

29

如果您想提交具有相同'name'属性的多行输入/选择到后端,那么您需要在这些输入(在表格行中)的名称值末尾添加方括号[]。

<form>
<input type="number" name="examplevar" />
<table>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
<table>
<input type="submit" />
</form>

这告诉您的浏览器为该名称属性创建一个数组。

在php中,将其解读为$_POST ['color_1'] [0]$_POST ['color_2'] [0],并按您喜欢的方式循环。

方括号在Phil Bailey的回答中出现,但没有指出。(加上是因为最近一次搜索让我找到了这个)


4
这段话告诉你的浏览器为该名称属性创建一个数组。 - JackWilson

4
为了提交表单,您需要添加 action 标签,它确定了表单提交后要转到的路径。
<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post">

当您想要POST值时,应指定带有特定名称的input字段。如果您只想让表格可见,您应该使用type hidden,以便表单将POST数据,但输入不可见。
<tr>
    <td>
        My value
        <input type="hidden" name="myValue" value="My value" />
    </td>
</tr>
一旦您的表单被提交,您可以在PHP文件中通过以下方式捕获POST数据:
//THE_PHP_FILE_TO_POST.php

if(isset($_POST))
{
    $myValue = $_POST['myValue']; //Contains the string "My value"
    //Do something with your POST
}

编辑 获取所有表格数据

if(isset($_POST))
{
    foreach($_POST as $inputName => $inputValue)
    {
        echo $inputName; //This is the name of an input field
        echo $inputValue; //This is the value of the input field 

    }
}

1
我有超过400个输入。这意味着我必须编写400行代码来获取表格数据。有没有更好的方法来实现这个目标? - Ali

2

PHP在表格输入字段方面有一个奇怪的命名约定,您需要将文件名指定为数组。对于以下使用PDO的SQL语句,可以使用一种处理方法。

select id,color_1,color_2,color_3,color_4 from colors;
<?php
// Get data
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_NUM);
reset($results);
echo '<table>';
while (list(, $i = each($results)) {
  echo  '<tr><td><input type=hidden name="id[]" value="' . $i[0] . '"></td>';
  echo '<td><input type=text name="color_1[]" value="'  . $i[1] . '"></td>';
  echo '<td><input type=text name="color_2[]" value="'  . $i[2] . '"></td>';
  echo '<td><input type=text name="color_3[]" value="'  . $i[3] . '"></td>';
  echo '<td><input type=text name="color_4[]" value="'  . $i[4] . '"></td>';
  echo '</tr>';
}
echo '</table>
?>

4条记录的简化 $_POST print_r 输出:

Array ( [[id] => Array ( [0] => 20 [1] => 21 [2] => 44 [3] => 45 ) 
[color_1] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue ) 
[color_2] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red ) 
[color_3] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue ) 
[color_4] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red ) )

1
您需要在表格中添加输入字段以发布数据。例如:
<input type="text"  name="fieldname" value="" />

如果你不想显示字段,那么可以使用type="hidden"使字段隐藏。
在表单中添加action属性。
Action属性指示数据将被发布的URL。

感谢尼克的回复。是的,每行有4个输入字段(每行有4个单元格),表中有数百行。因此,如果我通过php代码按名称获取值,那么我必须编写大量代码来获取100(行)* 4(输入字段)= 400个输入。所以我的问题是“如何最好地实现这一点”。 - Ali
最好将行数据与某些行ID/字段ID结合起来,以便将完整的行作为数组发布,从而减少名称。 - Bhavya Shaktawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接