diff src/site.js @ 12:3e2cb946d511

form handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Oct 2023 14:59:45 -0600
parents 62dd23f0b549
children
line wrap: on
line diff
--- a/src/site.js	Sun Oct 29 22:39:31 2023 -0600
+++ b/src/site.js	Mon Oct 30 14:59:45 2023 -0600
@@ -43,6 +43,41 @@
 	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
 };
 
+
+function ajaxForm(url,form) {
+	let post = '';
+	for( let i=0; i<form.length; i++ ) {
+		let input = form[i];
+		let name = input.name;
+		if( name === '' )
+			continue;
+		let type = input.type;
+		if( (type==='radio' || type==='checkbox') && !input.checked )
+			continue;
+		post += name + '=' + encodeURIComponent(input.value) + '&';
+	}
+	ajax(url,post,{form:form});
+}
+
+function clearErrors(form) {
+	let divs = form.querySelectorAll('div[error]');
+	for( let i=0; i<divs.length; i++ ) {
+		divs[i].textContent = '';
+	}
+}
+
+function showError(form,field,message) {
+	clearErrors(form);
+	let err = form.querySelector('[error="'+field+'"]');
+	err.textContent = message;
+	err.scrollIntoViewIfNeeded(false);
+	err.setAttribute('flash','');
+	setTimeout(function(){err.removeAttribute('flash')},2000);
+	let input = form.querySelector('input[name="'+field+'"]');
+	if(input) input.focus();
+}
+
+
 let currentPulldown = null;
 let newPulldown = null;