steve
steve

Reputation: 21

passing array data from an html form to php array variables

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.

In the form:

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

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

?>

Output from this is:

count of rhprid is 1

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

So:

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

Upvotes: 1

Views: 12194

Answers (1)

Shirin Abdolahi
Shirin Abdolahi

Reputation: 1067

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> 

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

?>

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

Upvotes: 1

Related Questions