我有一张表格来记录一组项目的工作时间。表单使用数组作为项目id,hours和notes字段,表单行是项目数量的循环。表单将数据传递给PHP脚本进行处理。 PHP脚本没有看到数组中的值...它只是给我“数组”作为输出。
文档和其他示例让我想知道我是否必须序列化或反序列化或其他什么来使事情正常工作,但多次尝试都没有成功。
表格形式:
<form action="input-hours-do.php" method="post">
<table>
<tr>
<td>Project</td>
<td>Hours</td>
<td>Notes</td>
</tr>
<?
// assign project IDs
$rhprID[0] = "ABT";
$rhprID[1] = "GHUR";
$rhprID[2] = "TRE";
$rhprID[3] = "WERT";
// loop on the projects
for ($i = 0; $i < 3; $i++)
{
?>
<!-- project id is not editable -->
<input type="hidden" name="rhprID[]" value="<? echo $rhprID[$i]; ?>" />
<tr>
<td>
<?php echo $rhprID[$i] ?>
</td>
<td>
<input type="text" name="rhHours[]" size="6" />
</td>
<td>
<input type="text" name="rhNotes[]" size="40" />
</td>
</tr>
<?
} // end of i for loop
?>
</table>
<input type="submit" name="tUpdate" size="8" value="Submit Hours" />
</form>
动作脚本中的(input-hours-do.php):
$rhprID = $_POST['rhprID'];
$rhHours = $_POST['rhHours'];
$rhNotes = $_POST['rhNotes'];
echo "count of prid is " . count($rhprID) . "</br>";
for ($k = 0; $k < count($rhprID); $k++)
{
echo " prID is: " . $rhprID[$k] . " hours are: " . $rhHours[$k] . " notes are: " . $rhNotes[$k];
}
?>
此处的输出是:
rhprid的数量是1
prID是:A小时:A笔记是:A
所以:
我需要更改什么来获取基础数据值?
答案 0 :(得分:1)
将php代码放在<?php ?>
代码而不是<? ?>
,这样就可以了:
<form action="input-hours-do.php" method="post">
<table>
<tr>
<td>Project</td>
<td>Hours</td>
<td>Notes</td>
</tr>
<?php /* changed <? to <?php */
// assign project IDs
$rhprID[0] = "ABT";
$rhprID[1] = "GHUR";
$rhprID[2] = "TRE";
$rhprID[3] = "WERT";
// loop on the projects
for ($i = 0; $i < 4; $i++) /* changed $i<3 to $i<4 */
{
?>
<!-- project id is not editable -->
<input type="hidden" name="rhprID[]" value="<?php /* changed <? to <?php */ echo $rhprID[$i]; ?>" />
<tr>
<td>
<?php /* changed <? to <?php */ echo $rhprID[$i] ?>
</td>
<td>
<input type="text" name="rhHours[]" size="6" />
</td>
<td>
<input type="text" name="rhNotes[]" size="40" />
</td>
</tr>
<?php /* changed <? to <?php */
} // end of i for loop
?>
</table>
<input type="submit" name="tUpdate" size="8" value="Submit Hours" />
</form>
并在input-hours-do.php文件中编写此代码:
<?php /* changed <? to <?php */
$rhprID = $_POST['rhprID'];
$rhHours = $_POST['rhHours'];
$rhNotes = $_POST['rhNotes'];
echo "count of prid is " . count($rhprID) . "</br>";
for ($k = 0; $k < count($rhprID); $k++)
{
echo " prID is: " . $rhprID[$k] . " hours are: " . $rhHours[$k] . " notes are: " . $rhNotes[$k];
}
?>
更新:您有4个项目ID,因此您必须在for循环中编辑$ i,使其小于4而不是3,