[英]Submit html table data with via form (post)
我想在提交按钮单击上发布此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输入。 所以我的问题是“实现这一目标的最佳途径是什么”
由于您尝试向后端提交具有相同“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>
这告诉浏览器为该name属性创建一个数组。
在php中,将其读作$_POST['color_1'][0]
和$_POST['color_2'][0]
,并根据需要循环播放。
括号在Phil Bailey的答案中,但没有指出。 (添加因为最近的搜索引导我这个)
要发布表单,您需要添加action
标记,该标记用于表示提交表单时要转到的路径
<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post">
如果要POST值,应指定具有特定name
input
字段。 如果您只希望表格可见,则应使用hidden
type
以便表单将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
}
}
对于表输入字段,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 ) )
您需要在表中添加输入字段以发布数据。 喜欢
<input type="text" name="fieldname" value="" />
如果您不想显示字段,则可以使用type="hidden"
字段
在表单中添加操作属性。
“操作”属性指示将在其上发布数据的网址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.