Hello,
Am trying using ajax to validate my form before submission and if false form submission should be canceled. so i have submit button calling the ajax function as (onclick="chkmyform('biodata' 'showError')") .
my ajax script below does validate the form correctly and display the error msg, but my problem is that form continue submitting if when it return false so the return false or true is not working, pls can som1 look at this script and tell me the problem with my return false or true? any help will be gratefull, tanks in advance
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function formchk(work1, showmsg) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
var Error_msg = req.responseText;
if(Error_msg !=""){
(document.getElementById('showmsg').innerHTML=req.responseText);
return false;
}else if (Error_msg ==""){(document.getElementById('showmsg').innerHTML="");
return true;
}
}
}
};
if(work1 == 'biodata'){
var sname=encodeURIComponent(document.getElementById("surname").value);
var fname=encodeURIComponent(document.getElementById("fname").value);
var add=encodeURIComponent(document.getElementById("address").value);
var town=encodeURIComponent(document.getElementById("town").value);
var sex=encodeURIComponent(document.getElementById("sex").value);
var state=encodeURIComponent(document.getElementById("state").value);
var lga=encodeURIComponent(document.getElementById("lga").value);
var phone=encodeURIComponent(document.getElementById("phone").value);
var email=encodeURIComponent(document.getElementById("email").value);
var params = "d="+work1+"&sname="+sname+"&fname="+fname+"&add="+add+"&town="+town+"&sex="+sex+"&state="+state+"&lga="+lga+"&email="+email+"&phone="+phone;
req.open("GET", "../myfiles/locale-form.php?"+params, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null);
}
}

