【问题标题】:Iterate Through HTML Array with PHP使用 PHP 遍历 HTML 数组
【发布时间】:2014-06-11 17:36:31
【问题描述】:

我正在尝试使用 php 创建一些表单。我被要求制作一个动态表单,只需单击一个按钮即可将输入行添加到表中。由于某种原因,我无法使用 php.ini 为每个动态表单行打印存储在数组中的值。

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

【问题讨论】:

  • 首先,关闭你的括号:$_POST['colleague']
  • 你的addRow函数是做什么的?请同时发布该代码;它可能不会产生你认为的那样。
  • 没有HTML array这样的东西。
  • @DavidSherret addRow 函数有效,但我还是会发布它。

标签: javascript php html arrays forms


【解决方案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>

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2011-03-06
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多