Answer the question
In order to leave comments, you need to log in
How to embed php into js?
I have an index.php file, it contains the body of the site with the form
<form action="#" method="POST">
<div class="dannue">
<input type="text" name="fio" placeholder="Имя" required="">
<input type="text" name="email" placeholder="Почта" required="">
<input type="hidden" name="token" id="token" value="03AGdBq26YYdWHX94usO0FU6">
</div>
<input type="text" name="thema" id="inp" placeholder="Тема" required="">
<textarea name="mess" cols="30" rows="10" placeholder="Сообщение" required=""></textarea>
<button id="send" type="submit">Отправить</button>
</form>
<script>
document.querySelector('form').addEventListener('submit', (e) =>{
e.preventDefault();
let tk = '';
grecaptcha.ready(function() {
grecaptcha.execute('6LfjGsMZAAAAAPxwEON9oAMztQGcRx', {action: 'homepage'}).then(function(token) {
tk = token;
document.getElementById('token').value = token;
const data = new URLSearchParams();
for(const pair of new FormData(document.querySelector('form'))){
data.append(pair[0], pair[1]);
}
fetch('send.php',{
method: 'post',
body: data,
})
.then(response => response.json())
.then(result => {
if(result['om_score'] >= 0.5){
console.log('человек');
<?php
$fio = $_POST['fio'];
$email = $_POST['email'];
$mess = $_POST['mess'];
$thema = $_POST['thema'];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$mess = htmlspecialchars($mess);
$thema = htmlspecialchars($thema);
$fio = urldecode($fio);
$thema = urldecode($thema);
$mess = urldecode($mess);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
if (mail("[email protected]", "Тема".$thema, "".$fio.". E-mail: ".$email. "сообщение:".$mess ,"[email protected] \r\n"))
{ header('Location: success-page.html');
} else {
header('Location: fail-page.html');
}
?>
}else{
console.log('бот');
}
});
});
});
});
</script>
<?php
$captcha;
if(isset($_POST['token'])){
$captcha = $_POST['token'];
}
$secretkey = '6LfjGsMZAAAAAAt9_XdcVXP-sfL8y1';
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secretkey.'&response='.$_POST['token'];
$response = file_get_contents($url);
$responsekeys = json_decode($response, true);
header('Content-type: application/json');
if($responsekeys["success"] && $responsekeys["score"] >= 0.5){
echo json_encode(array('success' => 'true', 'om_score' => $responsekeys["score"], 'token' => $_POST['token']));
}else {
echo json_encode(array('success' => 'false', 'om_score' => $responsekeys["score"], 'token' => $_POST['token']));
}
?>
<?php
$fio = $_POST['fio'];
$email = $_POST['email'];
$mess = $_POST['mess'];
$thema = $_POST['thema'];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$mess = htmlspecialchars($mess);
$thema = htmlspecialchars($thema);
$fio = urldecode($fio);
$thema = urldecode($thema);
$mess = urldecode($mess);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
if (mail("[email protected]", "Тема".$thema, "".$fio.". E-mail: ".$email. "сообщение:".$mess ,"[email protected] \r\n"))
{ header('Location: success-page.html');
} else {
header('Location: fail-page.html');
}
?>
Answer the question
In order to leave comments, you need to log in
Do not paste it, it works completely differently.
Your js is executed in the user's browser when they visit the page. And php code is executed on your server, BEFORE the page is sent.
If you want the user's actions to somehow affect the server, then you need to send a request to the server , using xhr or fectch, for example.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question