简体   繁体   English

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

[英]Submit html table data with via form (post)

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). 每行有4个输入字段(每行4个单元格)。 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. 因此,如果我通过PHP代码中的名称获取值,那么我必须编写大量代码以获得100(行)* 4(输入字段)= 400输入。 So my question was "What is the best way to achieve this" 所以我的问题是“实现这一目标的最佳途径是什么”

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). 由于您尝试向后端提交具有相同“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>

This tells your browser to create an array for that name property. 这告诉浏览器为该name属性创建一个数组。

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]$_POST['color_2'][0] ,并根据需要循环播放。

The brackets are in Phil Bailey's answer, but they are not pointed out. 括号在Phil Bailey的答案中,但没有指出。 (Added because a recent search led me to this) (添加因为最近的搜索引导我这个)

To post a form you need to add the action tag 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 input fields with a certain name . 如果要POST值,应指定具有特定name input字段。 If you only want the table is visible you should use the type hidden so the form will POST data, but the inputs aren't visible. 如果您只希望表格可见,则应使用hidden type以便表单将POST数据,但输入不可见。

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

EDIT Get all table data 编辑获取所有表格数据

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 

    }
}

PHP has a an odd naming convention for for table input fields you need to specify the name for the file as an array. 对于表输入字段,PHP有一个奇怪的命名约定,您需要将该文件的名称指定为数组。 For the following SQL Statement using PDO one processing method. 对于以下使用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>
?>

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

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 attribute indicates the url on which your data will be posted. “操作”属性指示将在其上发布数据的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM