Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Ajax form validation stop form submission

Javascript coding ..

Moderators: macek, egami, gesf

Ajax form validation stop form submission

Postby easyphp » Mon Jul 25, 2011 10:49 pm

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);

}



}
easyphp
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jul 25, 2011 10:40 pm

Re: Ajax form validation stop form submission

Postby easyphp » Mon Jul 25, 2011 10:51 pm

Not sure if the possition of my return false is correct

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;
}
easyphp
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jul 25, 2011 10:40 pm

Re: Ajax form validation stop form submission

Postby doji » Tue Aug 09, 2011 1:36 am

Where in the script are you referencing the AJAX library and how?
User avatar
doji
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue May 03, 2011 8:02 am
Location: Portland, Oregon


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron