发送 post 关联数组中我所有数据的请求

Sending post request of all my data in an associated array

我想创建一个 table,其中每一行都是 editable。同时我希望能够获取所有的数据行并将其放入一个文件中。

我遇到的问题是,当我单击带有 id = download 的按钮时,我无法获取所有行中的所有数据,只能获取第一行,就好像我单击了 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>

您不能在表单中嵌套表单,这是无效的 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()

从那一点开始处理你想要的方式