简体   繁体   中英

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:

  • it seems to think the array is only 1 element long
  • it's not getting to the values in the array ("A" is the first letter of "Array", so its just using the string variable, not the underlying data values).

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

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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