function checkAnswer(event){
event.preventDefault();
console.log(event.target);
let form = new FormData(event.target);
let options = {
method: "POST",
body:form
};
let URL = "АдресСкриптаКоторыйПроверяетОтветИВозвращаетНовыуюФормуДляВставки";
fetch(URL, options).then(function (response) {
return response.json();
}).then(function (response) {
console.log(response);
document.getElementById('quests').insertAdjacentHTML("beforeEnd", response.message);
}).catch(function (error) {
console.log(error);
});
}
<div id="quests">
<div class="middle">
<div id="content"></div>
<div class="pole">
<form name="perv1" onsubmit="checkAnswer(event)" class = "mid" action="index.php" method="post">
<input name="otvetp" type="text" placeholder="Впиши сюда ответ" >
<input type="submit" value="Отправить">
</form>
</div>
</div>
</div>
$html = '<div class="middle"><div id="content"></div><div class="pole"><form name="perv1" onsubmit="checkAnswer(event)" class = "mid" action="index.php" method="post"><input name="otvetp" type="text" placeholder="Впиши сюда ответ" ><input type="submit" value="Отправить"></form></div></div>';
$result = json_encode(["message"=>$html]);
echo $result;
$sum = $_POST["sum"];
if(!$sum){
$sum = json_encode(["message"=>"Вы ничего не написали"]);
} else {
$sum = json_encode(["message"=>$sum]);
}
echo $sum;
<script type= text/javascript>
function checkDataAjax(event){
event.preventDefault();
console.log(document.formName);
let form = new FormData(document.formName);
let options = {
method: "POST",
body:form
};
let URL = "АдресСервера";
fetch(URL, options).then(function (response) {
return response.json();
}).then(function (response) {
console.log(response);
document.getElementById('target').innerHTML = response.message;
}).catch(function (error) {
console.log(error);
});
}
{
$sum = json_encode(["message"=>"Вы ничего не написали"])
}
fetch(request, options).then(function (response) {
return response.json();
}).then(function (response) {
console.log(response)
document.getElementById('target').innerHTML = response.message;
}).catch(function (error) {
console.log(error);
});
fetch(request, options).then(function (response) {
return response.text();
}).then(function (response) {
console.log(response)
document.getElementById('target').innerHTML = response;
}).catch(function (error) {
console.log(error);
});