繁体   English   中英

使用PHP遍历HTML数组

[英]Iterate Through HTML Array with PHP

我正在尝试使用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>

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

在这里,尝试使用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>

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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