动态表单数组和循环时出现问题

时间:2013-12-23 03:43:35

标签: javascript php while-loop

我无法将数据输入动态表单以便正确返回。我不知道这是javascript还是PHP问题。

我的javascript:

    <script>
      function displayResult()
    {
        var table=document.getElementById("EquipTable");
        var row=table.insertRow(-1);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        cell1.innerHTML="<input type='text' name='EquipName[]' id='EquipName' size='40' />";
        cell2.innerHTML="<input type='text' name='Location[]' id='Location' size='10' />";
        cell3.innerHTML="<input type='text' name='Date[]' id='Date' size='10' />";
    }
    </script>

我的表格:

    <form id="form" name="form" method="post" action="equipaddsubmit.php">

    <table id="EquipTable">
      <tr>
        <td width="50%">Item Name</strong></td>
        <td width="25%">Location</strong></td>
        <td width="25%">Date Purchased</strong></td>
      </tr>
      <tr>
        <td valign="top">
           <input type="text" name="EquipName[]" id="EquipName" size="40" />
        </td>
        <td valign="top">
           <input type="text" name="Location[]" id="Location" size="10" />
        </td>
        <td valign="top">
           <input type="text" name="Date[]" id="Date" size="10" />
        </td>
      </tr>
    </table>
    <button type="button" onclick="displayResult()">Insert new row</button>
    <p><input type="submit" name="button" id="button" value="Submit" /></p>
    </form>

我的PHP在新页面上:

    $equip = (array) $_POST["EquipName"];
    $location = (array) $_POST["Location"];
    $date = (array) $_POST["Date"];
    $no_items = count($_POST['EquipName'])-1;
    $num=0;
    while($num<=$no_items)
      {
      $equip[$num]=$equip[$num];
      $location[$num]=$location[$num];
      $date[$num]=$date[$num];

    echo "Row" . $num . " ";
    echo $equip[$num] . " " ;
    echo $location[$num] . " ";
    echo $date[$num];
    echo "<br>";
    $num++;
    }

只有一行,数据会正确返回。只要我有多行,$ equip就会正确返回;其他的都是不可预测的。

感谢您的协助!

当我打印数组时,我得到以下内容:

Array ( 
 [EquipName] => Array ( [0] =>  Item1 [1] => Item2 )
 [Location] => Loc2 
 [Date] => Array ( [0] => 2013-01-02 ) 
 [button] => Submit 
)

Array ( 
 [EquipName] => Array ( [0] => Item1 [1] => Item2 )
 [Location] => Loc2 
 [Date] => Array ( [0] => 2013-01-02 ) 
 [button] => Submit 
)

0 个答案:

没有答案