feat: ajout de l'ajax pour le formulaire
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Simon 2025-05-07 14:53:45 +02:00
parent 927404e6fd
commit 3120050065
1 changed files with 34 additions and 1 deletions

View File

@ -1,5 +1,5 @@
<hr>
<form method="post" action="https://infolettre.lestoitsduval.fr/subscription/form" class="infolettre">
<form method="post" action="https://infolettre.lestoitsduval.fr/subscription/form" class="infolettre" id="infolettre">
<div>
<h2>Lettre d'information pour se tenir informé</h2>
<input type="hidden" name="nonce" />
@ -20,3 +20,36 @@
</div>
</div>
</form>
<script type="text/javascript">
var form = document.getElementById("infolettre");
function handleForm(e) {
e.preventDefault();
let formData = new FormData(this);
let parsedData = {};
for(let name of formData) {
if (typeof(parsedData[name[0]]) == "undefined") {
let tempdata = formData.getAll(name[0]);
if (tempdata.length > 1) {
parsedData[name[0]] = tempdata;
} else {
parsedData[name[0]] = tempdata[0];
}
}
}
let options = {};
switch (this.method.toLowerCase()) {
case 'post':
options.body = JSON.stringify(parsedData);
case 'get':
options.method = this.method;
options.headers = {'Content-Type': 'application/json'};
break;
}
fetch(this.action, options).then(r => r.json()).then(data => {
console.log(data);
});
}
form.addEventListener('submit', handleForm);
</script>