Отправка почтового запроса всех моих данных в связанном массиве

Я хочу создать таблицу, в которой она будет редактироваться в каждой строке. В то же время я хочу иметь возможность взять все строки данных и поместить их в файл.

Проблема, с которой я сталкиваюсь, заключается в том, что, когда я нажимаю кнопку с 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>

0

Решение

Вы не можете вкладывать формы в формы, это недопустимый 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() и т.п.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector