diff --git a/send-post-form-by-js/script.js b/send-post-form-by-js/script.js new file mode 100644 index 0000000..94bbfaa --- /dev/null +++ b/send-post-form-by-js/script.js @@ -0,0 +1,24 @@ +function sendPostWithData(url, data) { + // Создаем новый элемент формы + const form = document.createElement('form'); + form.setAttribute('method', 'post'); + form.setAttribute('action', url); + + // Добавляем скрытые поля для каждого параметра данных + Object.keys(data).forEach(function(key) { + const input = document.createElement('input'); + input.setAttribute('type', 'hidden'); + input.setAttribute('name', key); + input.setAttribute('value', data[key]); + form.appendChild(input); + }); + + // Добавляем форму на страницу (в конец body) + document.body.appendChild(form); + + // Отправляем форму + form.submit(); + + // Удаляем форму после отправки (не обязательно, но можно для очистки) + document.body.removeChild(form); +}