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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27314062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:18:31  来源:igfitidea点击:

Submit html table data with via form (post)

phpjqueryhtml

提问by Ali

I want to post this html table on submit button click.

我想在点击提交按钮时发布这个 html 表格。

01 - I want to retrieve the data of this table in php code (server side) 02 - I also want to display this table on another page.

01 - 我想在 php 代码(服务器端)中检索这个表的数据 02 - 我还想在另一个页面上显示这个表。

I'm using

我正在使用

PHP, JQuery

PHP、JQuery

I have html table with many rows in form tag.

我在表单标签中有很多行的 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>

===========

============

there are 4 input fields in each row (4 cells in each row). and hundred rows in table. So if I get value via name in php code then I have to write lot of code to get 100(rows) * 4 (input fields) = 400 inputs. So my question was "What is the best way to achieve this"

每行有 4 个输入字段(每行 4 个单元格)。和表中的一百行。因此,如果我通过 php 代码中的名称获得值,那么我必须编写大量代码才能获得 100(rows) * 4 (input fields) = 400 个输入。所以我的问题是“实现这一目标的最佳方法是什么”

回答by Kremnari

Since you are trying to submit multiple rows of inputs/selects with the same 'name' attribute to a backend, you would need to add square brackets [] to the end of the name value in those inputs (in the table rows).

由于您尝试将具有相同“名称”属性的多行输入/选择提交到后端,因此您需要将方括号 [] 添加到这些输入中名称值的末尾(在表行中)。

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

This tells your browser to create an array for that name property.

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

In php, read it as $_POST['color_1'][0]and $_POST['color_2'][0], and loop as you'd like.

在 php 中,将其读作$_POST['color_1'][0]and $_POST['color_2'][0],然后根据需要循环。

The brackets are in Phil Bailey's answer, but they are not pointed out. (Added because a recent search led me to this)

括号在 Phil Bailey 的回答中,但没有指出。(添加是因为最近的搜索使我想到了这一点)

回答by S.Pols

To post a form you need to add the actiontag which termines the path to go when the form is submitted

要发布表单,您需要添加一个action标签,该标签在提交表单时终止要走的路径

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

When you want to POST values you should specify inputfields with a certain name. If you only want the table is visible you should use the typehiddenso the form will POST data, but the inputs aren't visible.

当您想要 POST 值时,您应该使用input特定的name. 如果您只希望表格可见,您应该使用 ,typehidden这样表单将发布数据,但输入不可见。

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

Once your form is posted you can catch the POST data in that PHP file like this:

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

//THE_PHP_FILE_TO_POST.php

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

EDITGet all table data

EDIT获取所有表数据

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 

    }
}

回答by Phil Bailey

PHP has a an odd naming convention for for table input fields you need to specify the name for the file as an array. For the following SQL Statement using PDO one processing method.

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

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

Simplified $_POST print_r output for 4 records:

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

回答by Bhavya Shaktawat

You need to add input field in your table to post data. like

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

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

If you do not want to display field then you can make field hidden by using type="hidden"

如果您不想显示字段,则可以使用以下方法隐藏字段 type="hidden"

Add action attribute in your form.

在表单中添加 action 属性。

Action attribute indicates the url on which your data will be posted.

Action 属性表示您的数据将被发布到的 url。