从HTML表单传递数组数据到PHP数组变量

问题描述:

我有一个表格来记录一组项目的工作时间。形式为项目使用的ID,时间和备注字段的数组,表格线是在项目数量的循环。的形式将数据传递给处理PHP脚本。 PHP脚本没有看到该数组中......它只是给我的阵作为输出。

I have a form to record the hours worked on a set of projects. the form uses an array for the project id, hours, and a notes field, with the form lines being a loop on the number of projects. the form passes the data to a PHP script for processing. The PHP script is not seeing the values in the array... it's just giving me "Array" as the output.

文档和其他的例子有,如果我有序列化或反序列化或别的东西,把事情的工作权利,但多次尝试都没有成功我不知道。

Documentation and other examples have me wondering if I have to serialize or unserialize or something else to get things to work right, but multiple attempts have been unsuccessful.

在表单:

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

在动作脚本(输入小时-do.php):

in the action script (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

count of rhprid is 1

PRID是:一小时的有:A笔记有:A

prID is: A hours are: A notes are: A

所以:


  • 这似乎认为数组只有1元长

  • 它没有得到的数值数组中(A是阵列的第一个字母,所以它只是使用字符串变量,而不是底层的数据值)。

我需要什么改变来获得基础数据值?

What do I need to change to get to the underlying data values?

地方PHP codeS在&LT; PHP&GT; 标记不是&LT ;? ?&GT; ,它会就好了:

place php codes in <?php ?> tag not <? ?>, and it will be just fine:

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

和输入小时,do.php文件写这篇code:

and in input-hours-do.php file write this code :

<?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,所以你必须在$ I编辑循环小于4不是3,

UPDATE : you have 4 project id,so you have to edit $i in your for loop to be less than 4 not 3,