Пример простой формы на HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <head> <title>HTML button formaction</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <div> <form action="formaction.php" method="post" enctype="application/x-www-form-urlencoded"> <div>Field:<input type="text" name="field1" value="simplevalue" /></div> <button>Отправить</button> </form> </div> </body> </html> |
Эта форма отправляет POST-запрос на formaction.php. Атрибут enctype указывать не обязательно, так как он по умолчанию имеет это значение.
Теперь допустим, что мы хотим добавить ещё одну кнопку, но отправляющую форму на другой url=buttonaction.php. Для этого нужно использовать атрибут formaction у тега button. Вот так:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <head> <title>HTML button formaction</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <div> <form action="formaction.php" method="post" enctype="application/x-www-form-urlencoded"> <div>Field:<input type="text" name="field1" value="simplevalue" /></div> <button>Отправить</button> <button formaction="buttonaction.php">Отправить на buttonaction</button> </form> </div> </body> </html> |
На formaction.php может быть следующий код:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <title>HTML button formaction</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <div> <H1>formaction.php</H1> <p>Field value: <?php print $_REQUEST['field1']; ?></p> <p> <a href="index.html">На форму ввода</a> </p> </div> </body> </html> |
И на buttonaction.php следующий:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <title>HTML button formaction</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <div> <H1>buttonaction.php</H1> <p>Field value: <?php print $_REQUEST['field1']; ?></p> <p> <a href="index.html">На форму ввода</a> </p> </div> </body> </html> |
Теперь при нажатии кнопки «Отправить» выполнение передаётся на formaction.php, а при нажатии «Отправить на buttonaction» — buttonaction.php.
Работающий пример можно посмотреть по ссылке — https://urvanov.ru/projects/html-examples/button-formaction/index.html
Спасибо за код и в целом гайд 🙂