【问题标题】:Sending post request of all my data in an associated array在关联数组中发送我所有数据的发布请求
【发布时间】:2017-09-04 02:53:32
【问题描述】:

我想创建一个表格,在该表格的每一行中都可以对其进行编辑。同时我希望能够把所有的数据行放到一个文件中。

我遇到的问题是,当我单击 id = 下载的按钮时,我无法获取所有行中的所有数据,而只能获取第一行,就好像我单击了 id = firstRow 的提交按钮一样。

有人知道如何通过单击按钮检索所有数据行吗? 我目前只使用 PHP,因此使用 PHP 或 HTML 来解决这个问题会很有帮助。

<form method="post" action="allRows.php">
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Single</th>
            <th>Test button</th>
        </tr>
        <tr>
            <form action="oneRow.php" method="post">
                <td><input type="text" name="person[0][name]"></td>
                <td><input type="text" name="person[0][age]"></td>
                <td><input type="text" name="person[0][sex]"></td>
                <td><input type="text" name="person[0][spouse]"></td>
                <td><input id="firstRow" type="submit" name="test"></td>
            </form>
        </tr>
        <tr>
            <form action="oneRow.php" method="post">
                <td><input type="text" name="person[1][name]"></td>
                <td><input type="text" name="person[1][age]"></td>
                <td><input type="text" name="person[1][sex]"></td>
                <td><input type="text" name="person[1][spouse]"></td>
                <td><input id="secondRow" type="submit" name="test"></td>
            </form>
        </tr>
    </table>
    <input id="download" type="submit" name="Download" value="Download">
</form>

【问题讨论】:

  • 仅使用 1 个&lt;form&gt; 标签。
  • 仅使用 1 个表单标签,我无法单独编辑每一行。

标签: php html forms download submit


【解决方案1】:

你不能在表单中嵌套表单,那是无效的 html,所以如果你想拥有所有这些功能,你需要使用 javascript 通过将它们组装成一个 js 表单来一次提交所有表单,但纯 PHP 是除非您一次提交一份包含所有字段的表单,否则这是不可能的。

<!-- You need the jQuery library -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Single</th>
            <th>Test button</th>
        </tr>
        <tr>
            <!-- You need to add class="rowform" to each form tag -->
            <form action="oneRow.php" method="post" class="rowform">
                <td><input type="text" name="person[0][name]"></td>
                <td><input type="text" name="person[0][age]"></td>
                <td><input type="text" name="person[0][sex]"></td>
                <td><input type="text" name="person[0][spouse]"></td>
                <td><input id="firstRow" type="submit" name="test"></td>
            </form>
        </tr>
        <tr>
            <form action="oneRow.php" method="post" class="rowform">
                <td><input type="text" name="person[1][name]"></td>
                <td><input type="text" name="person[1][age]"></td>
                <td><input type="text" name="person[1][sex]"></td>
                <td><input type="text" name="person[1][spouse]"></td>
                <td><input id="secondRow" type="submit" name="test"></td>
            </form>
        </tr>
    </table>
<!-- Create the empty download all form, add id="allrows" -->
<form method="post" action="allRows.php" id="allrows">
    <input id="download" type="submit" name="Download" value="Download">
</form>

<script>
// Start document listener
$(document).ready(function(e) {
    // Listen for the download form to submit
    $('#allrows').on('submit',function(e) {
        // Stop it from reloading the page (submitting the form)
        e.preventDefault();
        // Create a storage array
        var data = [];
        // Loop through each form (each form tag needs the "rowform" class
        $.each($('.rowform'),function(k,v) {
            // Fetch all the data from the form
            data[k] = $(v).serialize();
        });
        // Create a storage array for the form
        var form = [];
        // Start building a form
        form.push('<form action="allRows.php" method="post">');
        // Implode the form data from each form
        form.push('<input name="allfields[]" value="'+data.join('" /><input name="allfields[]" value="')+'" />');
        // Create a submit field
        form.push('<input type="submit" value="submit" /></form>');
        // Combine the html form
        form    =   form.join('');
        // Submit the form
        $(form).submit();
    });
});
</script>

在 php 中,您需要检查 allfields 键,所以:

if(!empty($_POST['allfields'])) {
    // do code
}

你会看到类似于:

Array
(
    [allfields] => Array
        (
            [0] => person%5B0%5D%5Bname%5D=qewrqwer&person%5B0%5D%5Bage%5D=adsf&person%5B0%5D%5Bsex%5D=fdsdfds&person%5B0%5D%5Bspouse%5D=sdfds
            [1] => person%5B1%5D%5Bname%5D=sssssss&person%5B1%5D%5Bage%5D=sssweeeee&person%5B1%5D%5Bsex%5D=qqqqqq&person%5B1%5D%5Bspouse%5D=222222
        )
)

您会看到该字段有一系列数组和查询字符串。使用urldecode() 等处理您想要的方式。

【讨论】:

  • 我当然需要更多地了解 Javascript。现在,我通过编写另一组隐藏的输入并一次检索所有数据并将其放入表单中来做到这一点。通过这样做可以避免这种嵌套形式。
猜你喜欢
  • 2018-03-15
  • 2018-08-04
  • 2014-09-02
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多