在关联的数组中发送所有数据的发布请求

时间:2017-09-04 02:53:33

标签: php html forms download submit

我想创建一个表格,在每个行中都可以编辑它。同时我希望能够获取所有数据行并将其放入文件中。

我遇到的问题是当我点击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>

1 个答案:

答案 0 :(得分: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()等处理您想要的方式