passing array data from an html form to php array variables

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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?

回答1:

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,



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!