使用PHP迭代HTML数组

时间:2014-06-11 17:36:31

标签: javascript php html arrays forms

我试图用php创建一些表单。我被要求创建一个动态表单,在单击按钮时将输入行添加到表中。由于某种原因,我无法使用php打印每个动态表单行的数组中存储的值。

<html>
    <body>
        Welcome <?php echo $_POST["name"]; ?></br>
        You e-mail address is <?php echo $_POST["email"];?></br>
        Your occupation is <?php echo $_POST["occupation"];?></br>

<?php foreach($_POST['colleague' as $a){ ?>
        Your colleague is <?php echo $a;?></br>
<?php }?>
   </body>
</html>

php代码应该打印在表单中提交的所有同事值,但由于某种原因它没有这样做。

<html>
    <script src="script.js"></script>
    <body>
        <form action="test.php" method="post">
        <table id="testTable" style="text-align:left">
            <tr>
                <th>
                Name
                </th>
                <td>
                <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <th>
                Email
                </th>
                <td>
                <input type="text" name="email"
                </td>
            </tr>
            <tr>
                <th>
                Occupation
                </th>
                <td>
                <input type = "text" name="occupation">
                </td>
            </tr>
            <tr>
                <th>
                Colleague
                </th>
                <td>
                <input type ="text" name="colleague[]">
                </td>
                <td>
                    <input type="button" value="Add Colleague" onClick="addRow('testTable')" />
                </td>
            </tr>
        </table>
        <input type="submit">
        </form>
    </body>
</html>

&#39;

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var header = table.createTHead();
    var row = table.insertRow(rowCount);
    var cell = row.insertCell(0);
    header.innerHTML = table.rows[3].cells[0].innerHTML;
    cell.appendChild(header);
    var cellTwo = row.insertCell(1);
    cellTwo.innerHTML = table.rows[3].cells[1].innerHTML;
}

1 个答案:

答案 0 :(得分:1)

在这里,尝试使用HTML

<html>
    <script src="script.js"></script>
    <body>
        <form action="test.php" method="post">
        <table id="testTable" style="text-align:left">
            <tr>
                <th>
                Name
                </th>
                <td>
                <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <th>
                Email
                </th>
                <td>
                <input type="text" name="email">
                </td>
            </tr>
            <tr>
                <th>
                Occupation
                </th>
                <td>
                <input type = "text" name="occupation">
                </td>
            </tr>
            <tr>
                <th>
                Colleague
                </th>
                <td>
                <input type ="text" name="colleague[]">
                </td>
                <td>
                    <input type="button" value="Add Colleague" onClick="addRow('testTable')" />
                </td>
            </tr>
        </table>
        <input type="submit">
        </form>
    </body>
</html>

对于PHP文件(test.php)编写

<html>
    <body>
        Welcome <?php echo $_POST["name"]; ?><br />
        You e-mail address is <?php echo $_POST["email"];?><br />
        Your occupation is <?php echo $_POST["occupation"];?><br/>

<?php foreach($_POST['colleague'] as $a){ ?>
        Your colleague is <?php echo $a;?></br>
<?php }?>
    </body>
</html>

希望它有所帮助。