From fe2a7463d15dccd8da261744ec05721ab8c38a42 Mon Sep 17 00:00:00 2001 From: Aliaksei Karzhou Date: Thu, 11 Jul 2024 17:16:13 +0300 Subject: [PATCH] feat: added php script --- assets/js/modals.js | 28 ++++++++++++++++------------ send.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 send.php diff --git a/assets/js/modals.js b/assets/js/modals.js index f2b7484..6cc6521 100644 --- a/assets/js/modals.js +++ b/assets/js/modals.js @@ -31,22 +31,26 @@ const CHAT_ID = ''; const formData = new FormData(event.currentTarget); const name = formData.get('name'); const phone = formData.get('phone'); - const message = `Имя: ${name} Телефон ${phone}`; - sendMessage(message); + // const message = `Имя: ${name} Телефон ${phone}`; + // sendMessage(message); event.currentTarget.reset(); + fetch('../../send.php', { + method: 'POST', + data: { name, phone }, + }); if (callbackModal) callbackModal.classList.remove('active'); if (thanksModal) thanksModal.classList.add('active'); }); }); -function sendMessage(message) { - const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&parse_mode=html&text=${message}`; +// function sendMessage(message) { +// const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&parse_mode=html&text=${message}`; - fetch(url) - .then((data) => { - console.log('Message sent successfully:', data); - }) - .catch((error) => { - console.error('Error sending message:', error); - }); -} +// fetch(url) +// .then((data) => { +// console.log('Message sent successfully:', data); +// }) +// .catch((error) => { +// console.error('Error sending message:', error); +// }); +// } diff --git a/send.php b/send.php new file mode 100644 index 0000000..358640c --- /dev/null +++ b/send.php @@ -0,0 +1,36 @@ + $name, + 'Телефон:' => $phone + ); + +//Настраиваем внешний вид сообщения в телеграме + foreach($arr as $key => $value) { + $txt .= "".$key." ".$value."%0A"; + }; + +//Передаем данные боту + $sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r"); + +//Выводим сообщение об успешной отправке + if ($sendToTelegram) { + alert('Спасибо! Ваша заявка принята. Мы свяжемся с вами в ближайшее время.'); + } + +//А здесь сообщение об ошибке при отправке + else { + alert('Что-то пошло не так. ПОпробуйте отправить форму ещё раз.'); + } +} + +?> \ No newline at end of file