submit javascript

This commit is contained in:
Yannik Schmidt
2021-08-30 18:38:34 +02:00
parent ea91f808eb
commit 627d249ebb

58
static/submit.js Normal file
View File

@@ -0,0 +1,58 @@
function submitForm(){
/* check input fields */
nameField = document.getElementById("name")
passwordField = document.getElementById("password")
nameField.style.borderColor = "gray"
passwordField.style.borderColor = "gray"
invalidField = false
if(nameField.value == ""){
nameField.style.borderWidth = "2px"
nameField.style.borderColor = "red"
invalidField = true
}
if(passwordField.value == ""){
passwordField.style.borderWidth = "2px"
passwordField.style.borderColor = "red"
invalidField = true
}
if(invalidField){
return
}
/* show the waiting dialog
dialog = document.getElementById("waiting-dialog")
dialog.style.disply = "block"
setMainBackgroundOpacity(0.5)
*/
/* submit the form */
xhr = new XMLHttpRequest();
xhr.open("POST", "/create-user");
xhr.onload = formSubmitFinished
formData = new FormData(document.getElementById("user-form"));
xhr.send(formData);
}
function formSubmitFinished(event){
if(event.target.status < 200 || event.target.status >= 300){
showErrorMessage(event.target); // blocking
}else{
window.location.href = event.target.responseText
}
}
/*
function setMainBackgroundOpacity(opacity){
mainContainer = document.getElementById("main-container")
mainContainer.style.opacity = opacity
}
*/
function showErrorMessage(target){
alert(target.responseText)
}