Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2012, 09:53 PM   PM User | #1
Rappa
Regular Coder

 
Rappa's Avatar
 
Join Date: Nov 2005
Location: LI
Posts: 356
Thanks: 21
Thanked 9 Times in 9 Posts
Rappa is an unknown quantity at this point
jQuery AJAX Submit handler question

So I have a log in form and I created this function for the submit:

Code:
$('#logonform').submit(function() {		
		errortxt="";
		errors=0;
		if ($('#usernamelogin').val() == "") {errors++; errortxt += "<li>Please enter your <strong>Username</strong>.</li>";}
		else if ($('#usernamelogin').val().length < 5) {errors++; errortxt += "<li>Your <strong>Username</strong> must be at least 5 characters long.</li>";}
		if ($('#passwordlogin').val() == "") {errors++; errortxt += "<li>Please enter your <strong>Password</strong>.</li>";}
		
		var usernamelogin = $('#usernamelogin').val();
		var passlogin = hex_md5($('#passwordlogin').val());
		var dataString = "userdata="+usernamelogin+"&amp;"+"pwdata="+passlogin;
		
		if (errors != 0) {
			$("#loginerr .formee-msg-error ul").html(errortxt); 
			$("#loginerr").css('display','block'); 
			return false;
		} else {
			$("#logstatus").html('<img align="absmiddle" src="images/loader.gif" /> Hmmm..');
			$.post('logincheck.php',{userdata:usernamelogin, pwdata:passlogin}, function (data) {
				if (data == 'NO') {
					errortxt += "<li>Your <strong>Username</strong> or <strong>Password</strong> are incorrect.</li>";
					$('#logstatus').html("");
					$("#loginerr .formee-msg-error ul").html(errortxt); 
					$("#loginerr").css('display','block'); 
					return false;
				} else if (data == 'OK') {
					$("#loginerr").css('display','none');
					$('#logstatus').html("");
					return true;
				}
			});
			return false;
		}
	});
Basically it validates the fields, if they dont validate it shows the error log, if it does it uses ajax to grab either a 'NO' or 'OK' for validating the user/pw in the database. However I can not get it to work. Ive verified the php script works fine. But im having trouble with the ajax response.

That last return false lets the script run sort of correctly, where as if your credentials are wrong it does display the error message, however if they are not and it hits the return true, it does not run the form action, it just hits the return false again.

But if you remove that last return false it doesnt work at all, it runs the form action valid or not.

Please help.

(Also dont worry about the hex_md5() javascript function, theres an external script handling that)
Rappa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:17 PM.


Advertisement
Log in to turn off these ads.