Last active
April 22, 2024 14:30
-
-
Save HDDen/b7f3028fb9747039b8144000aef50316 to your computer and use it in GitHub Desktop.
Custom form submit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
js: | |
/** | |
* Form submit | |
*/ | |
document.querySelectorAll('form').forEach(function(el){ | |
el.addEventListener('submit', function(e){ | |
var path = window.location.pathname+'php/sender.php'; | |
var debug = false; | |
e.preventDefault(); | |
// grab values | |
var counter = 1; | |
var values = Object.values(this).reduce(function(obj,field){ | |
try { | |
if (!field || !(field instanceof HTMLElement)){ | |
return obj; | |
} | |
var name = field.getAttribute('name'); | |
if (!name){ | |
if (field.id) { | |
name = field.id; | |
} else if (field.className) { | |
name = field.className.replace(' ', '__'); | |
} else { | |
name = 'field_'+counter; | |
counter++; | |
} | |
} | |
obj[name] = field.value; | |
} catch (error){ | |
console.log(error); | |
} | |
return obj; | |
}, {}); | |
if (debug) console.log(values); | |
// send values | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function (){ | |
xhr__callback(this.responseText); | |
} | |
xhr.onerror = function (){ | |
} | |
xhr.open("POST", path, true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.send(JSON.stringify(values)); | |
// sending callback | |
function xhr__callback(data){ | |
data = JSON.parse(data); | |
if (debug) console.log('xhr__callback:', data); | |
} | |
}); | |
}); | |
php: | |
<?php | |
$debug = false; | |
$mailTo = '[email protected]'; | |
$subject = 'Заявка с сайта '.$_SERVER['HTTP_HOST']; | |
$from = 'admin@'.$_SERVER['HTTP_HOST']; | |
$message = ''; | |
$postData = file_get_contents('php://input'); | |
$data = json_decode($postData, true); | |
if ($debug) { | |
echo json_encode($data); | |
} | |
/** | |
* Sending email | |
*/ | |
if ($data){ | |
$aliases = [ | |
'name' => 'Имя', | |
'phone' => 'Телефон', | |
'calltime' => 'Время для звонка', | |
'agree' => 'Согласие на обработку', | |
'site' => 'Сайт', | |
'wishes' => 'Пожелания', | |
'question' => 'Вопрос', | |
]; | |
foreach ($data as $key => $value){ | |
if ($message) $message .= '<br> '; | |
$normalised_key = $key; | |
if (array_key_exists($key, $aliases)){ | |
$normalised_key = $aliases[$key]; | |
} | |
$message .= $normalised_key . ': '.$value; | |
} | |
} | |
// add ip | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
$message .= '<br> IP: '.$ip; | |
// headers | |
$headers = 'MIME-Version: 1.0' . "\r\n"; | |
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; | |
$headers .= 'From: ' . $from . "\r\n"; | |
mail($mailTo, $subject, $message, $headers); | |
/** | |
* json echo | |
*/ | |
$response = [ | |
'status' => 'ok', | |
'status__message' => 'Спасибо за Ваше обращение!', | |
'response__html' => '', | |
'response__data' => [], | |
]; | |
echo json_encode($response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment