通过via表单提交html表数据(post)

时间:2014-12-05 10:42:23

标签: php jquery html

我想在提交按钮点击时发布这个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输入。所以我的问题是&#34;实现这一目标的最佳途径是什么?

4 个答案:

答案 0 :(得分:15)

由于您尝试使用相同的名称&#39;提交多行输入/选择。属性到后端,您需要在这些输入(表格行)中的名称值末尾添加方括号[]。

<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的回答,但没有指出。 (添加因为最近的搜索引导我这个)

答案 1 :(得分:4)

要发布表单,您需要添加action标记,该标记用于表示提交表单时要去的路径

<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post">

如果要POST值,则应指定具有特定input的{​​{1}}个字段。如果您只希望表格可见,则应使用name type,以便表单将POST数据,但输入不可见。

hidden

发布表单后,您可以捕获该PHP文件中的POST数据,如下所示:

<tr>
    <td>
        My value
        <input type="hidden" name="myValue" value="My value" />
    </td>
</tr>

编辑获取所有表格数据

//THE_PHP_FILE_TO_POST.php

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

答案 2 :(得分: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 ) )

答案 3 :(得分:1)

您需要在表格中添加输入字段以发布数据。 喜欢

<input type="text"  name="fieldname" value="" />

如果您不想显示字段,则可以使用type="hidden"

隐藏字段

在表单中添加操作属性。

操作属性指示将在其上发布数据的网址。

相关问题