comparison 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
comparison
equal deleted inserted replaced
11:62dd23f0b549 12:3e2cb946d511
41 window.err = null; 41 window.err = null;
42 } 42 }
43 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); 43 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
44 }; 44 };
45 45
46
47 function ajaxForm(url,form) {
48 let post = '';
49 for( let i=0; i<form.length; i++ ) {
50 let input = form[i];
51 let name = input.name;
52 if( name === '' )
53 continue;
54 let type = input.type;
55 if( (type==='radio' || type==='checkbox') && !input.checked )
56 continue;
57 post += name + '=' + encodeURIComponent(input.value) + '&';
58 }
59 ajax(url,post,{form:form});
60 }
61
62 function clearErrors(form) {
63 let divs = form.querySelectorAll('div[error]');
64 for( let i=0; i<divs.length; i++ ) {
65 divs[i].textContent = '';
66 }
67 }
68
69 function showError(form,field,message) {
70 clearErrors(form);
71 let err = form.querySelector('[error="'+field+'"]');
72 err.textContent = message;
73 err.scrollIntoViewIfNeeded(false);
74 err.setAttribute('flash','');
75 setTimeout(function(){err.removeAttribute('flash')},2000);
76 let input = form.querySelector('input[name="'+field+'"]');
77 if(input) input.focus();
78 }
79
80
46 let currentPulldown = null; 81 let currentPulldown = null;
47 let newPulldown = null; 82 let newPulldown = null;
48 83
49 function clickMenu(clicked,display) { 84 function clickMenu(clicked,display) {
50 //console.log("clickMenu"); 85 //console.log("clickMenu");