Как отправить данные в форму html из строки при нажатии на нее?
У меня есть таблица с данными и при нажатии на кнопку "Редактировать" открывается страница уже с заполненными данными из той строки, на которой была нажата кнопка "Редактировать", но можно ли как-то сделать чтобы было без кнопки, а просто при нажатии на строку? Вот код с кнопкой: $content = '<table> <thead> <tr> <th>Название</th> <th>Адрес</th> <th>ИНН</th> <th>КПП</th> <th>Расчетный счет</th> <th>ОГРН</th>'; if ($user_role === 'Администратор' || $user_role === 'Специалист'){ $content .= '<th>Действия</th>'; } $content .= '</tr> </thead> <tbody>'; foreach ($organizations as $organization){ $content .= '<tr> <td>' . htmlspecialchars($organization['okopf'] ??
'') .' '.'"'. htmlspecialchars($organization['name']) . '"'.'</td> <td>' . htmlspecialchars($organization['address']) . '</td> <td class="th-center">' . htmlspecialchars($organization['inn']) . '</td> <td class="th-center">' . htmlspecialchars($organization['kpp']) . '</td> <td class="th-center">' . htmlspecialchars($organization['settlement_account']) . '</td> <td class="th-center">' . htmlspecialchars($organization['ogrn']) . '</td>'; if ($user_role === 'Администратор' || $user_role === 'Специалист'){ $content .= '<td class="th-center"> <form method="POST" action="organizations/edit_organizations.php" > <input type="hidden" name="action" value="edit_f"> <input type="hidden" name="id" value="' . $organization['id'] . '"> <button type="submit" class="action-button">Редактировать</button> </form> </td>'; } $content .= '</tr>'; } $content .= '</tbody> </table>'; Но я смог убрать кнопку и сделать так, чтобы можно было нажимать на строку с помощью jQuery, но форма не отправляет ID данной строки, и, соответственно, данные со строки автоматически не вставляются, как это было с кнопкой. Как мне сделать, чтобы при нажатие на кнопку отправлялась форма? Вот код без кнопки: $content = ' <table> <thead> <tr> <th>Название</th> <th>Адрес</th> <th>ИНН</th> <th>КПП</th> <th>Расчетный счет</th> <th>ОГРН</th>'; $content .= '</tr> </thead> <tbody>'; foreach ($organizations as $organization){ $content .= '<tr data-href="../organizations/edit_organizations.php" style="cursor: pointer"> <td>' . htmlspecialchars($organization['okopf'] ?? '') .' '.'"'. htmlspecialchars($organization['name']) . '"'.'</td> <td>' . htmlspecialchars($organization['address']) . '</td> <td class="th-center">' . htmlspecialchars($organization['inn']) . '</td> <td class="th-center">' . htmlspecialchars($organization['kpp']) . '</td> <td class="th-center">' . htmlspecialchars($organization['settlement_account']) . '</td> <td class="th-center">' . htmlspecialchars($organization['ogrn']) . '</td> <form method="POST" action="../organizations/edit_organizations.php" > <input type="hidden" name="action" value="edit_f"> <input type="hidden" name="id" value="' . $organization['id'] . '"> </form>'; $content .= '</tr>'; } $content .= '</tbody> </table>'; Нашел еще один способ сделать просто через JavaScript, при котором при нажатии на строку tr отправлять форму, но единственная проблема что функция отрабатывает только 1 раз, из-за малого опыта не знаю как заставить отправлять функцию n-количество раз (строки всегда будут меняться). Вот код. <table> <thead> <tr> <th>Название</th> <th>Адрес</th> <th>ИНН</th> <th>КПП</th> <th>Расчетный счет</th> <th>ОГРН</th>'; $content .= '</tr> </thead> <tbody>'; foreach ($organizations as $organization){ $content .= '<tr data-href="../organizations/edit_organizations.php" style="cursor: pointer" id="post"> <td>' . htmlspecialchars($organization['okopf'] ?? '') .' '.'"'. htmlspecialchars($organization['name']) . '"'.'</td> <td>' . htmlspecialchars($organization['address']) . '</td> <td class="th-center">' . htmlspecialchars($organization['inn']) . '</td> <td class="th-center">' . htmlspecialchars($organization['kpp']) . '</td> <td class="th-center">' . htmlspecialchars($organization['settlement_account']) . '</td> <td class="th-center">' . htmlspecialchars($organization['ogrn']) . '</td> <form method="POST" action="../organizations/edit_organizations.php" id="formpost"> <input type="hidden" name="action" value="edit_f"> <input type="hidden" name="id" value="' . $organization['id'] . '"> </form>'; $content .= '</tr>'; } $content .= '</tbody> </table>'; Вот скрипт: <script> document.getElementById("post").onclick = function() { document.getElementById("formpost").submit(); } </script>
Источник: ru.stackoverflow.com — ru.stackoverflow.com/questions/1616378/%D0%9A%D0%B0%D0%BA-%D0%BE%D1%82%D0%BF%D1%80%D0%B0%D0%B2%D0%B8%D1%82%D1%8C-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D0%B5-%D0%B2-%D1%84%D0%BE%D1%80%D0%BC%D1%83-html-%D0%B8%D0%B7-%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B8-%D0%BF%D1%80%D0%B8-%D0%BD%D0%B0%D0%B6%D0%B0%D1%82%D0%B8%D0%B8-%D0%BD%D0%B0-%D0%BD%D0%B5%D0%B5