/ Published in: JavaScript
We send a form via an ajax call after some basic validation checks based upon whether the element was wrapped in a div tag with a class of required. Subsequently we post it via out makeRequestPost ajax routine, updating the targetDiv appropriately.
Expand |
Embed | Plain Text
// JavaScript Document //David Aspden Dec 08 //Can we force actions to Ajax calls for myForms? function overRideSubmit(myForm,targetDivId){ if(quickValidate(myForm)){ if(typeof(target) == "string"){ var targetDiv = document.getElementById(targetDiv); } else{ targetDiv = target; } var action = myForm.action; var method = myForm.method; //So we know what would have happened, re route through the ajax request. if(method=="get"){ var getString = buildParameterString(myForm); if(action.indexOf('?') == -1){ getString = '?'+getString; } else{ getString = '&'+getString; } makeRequesttoObject(action+getString, targetDiv); } if(method=="post"){ var postStr = buildParameterString(myForm); makeRequestPOST(action, postStr, targetDiv); } } return false; } //This will be the getString or PostParameters function buildParameterString(myForm){ var parameterString=""; var radioParams = new Object; for(i=0; i<myForm.length; i++){ if(myForm.elements[i].type !='submit' | myForm.elements[i].type !='reset' | myForm.elements[i].type !='button' | myForm.elements[i].type !='image' | myForm.elements[i].type !='file'){ if(myForm.elements[i].type =='text' | myForm.elements[i].type =='hidden'){ parameterString+=myForm.elements[i].name+'='+encodeURI(myForm.elements[i].value)+'&'; } if(myForm.elements[i].type =='radio'){ //Radio Buttons ->With same name... if(!radioParams[myForm.elements[i].name]){ //Ensure we capture the fact that it is there. radioParams[myForm.elements[i].name] = ""; } if (myForm.elements[i].checked==true){ //Capture value radioParams[myForm.elements[i].name] = encodeURI(myForm.elements[i].value); } else{ //We do not add to the radioParams object } //Delay Adding Rad Vals until the end in case radio values are in different places } if(myForm.elements[i].type =='checkbox'){ //Checkbox Checks if (myForm.elements[i].checked == true){ var check_val = 1; } else{ var check_val = 0; } // parameterString+=myForm.elements[i].name+'='+encodeURI(check_val)+'&'; } } } //parse Radio Array var radios=""; for(prop in radioParams){ radios += prop+'='+radioParams[prop]+'&'; } urlString = parameterString.substr(0,parameterString.length-1)+'&'+radios return urlString.substr(0, urlString.length-1); } //We just check that things are actually filled in. function quickValidate(myForm){ for(i=0; i<myForm.length; i++){ if(myForm.elements[i].parentNode.tagName == 'DIV'){ if((myForm.elements[i].parentNode.className == "required" | myForm.elements[i].parentNode.className == 'required_false') && myForm.elements[i].value ==''){ myForm.elements[i].parentNode.className = "required_false" return false; } if(myForm.elements[i].parentNode.className == 'required_false' && myForm.elements[i].value !=''){ myForm.elements[i].parentNode.className = 'required'; } } if(myForm.elements[i].parentNode.tagName == 'LABEL'){ if(myForm.elements[i].parentNode.parentNode.tagName == 'DIV'){ if((myForm.elements[i].parentNode.parentNode.className == 'required' | myForm.elements[i].parentNode.parentNode.className == 'required_false') && myForm.elements[i].value ==''){ //Should be a div myForm.elements[i].parentNode.parentNode.className = 'required_false'; return false; } if((myForm.elements[i].parentNode.parentNode.className == 'required_false') && myForm.elements[i].value !=''){ myForm.elements[i].parentNode.parentNode.className = 'required'; } } } } //For Loop return true; }
You need to login to post a comment.
