GSimpson
03-21-2010, 04:54 AM
Hey there.
I've recently adapted to the jQuery library to start making my websites more dynamic, and so far it's gone really well, and I've even started validating forms using jQuery, but I can't seem to get my head around the server side functions in relation to the jQuery ajax functions.
Alright so my problem is that I have a form, and on submission it validates all of the fields using client side methods such as javascript's length() object. I'm using the basic "if, if else, else" methods to work through the list of things to validate and that seems to work well.
if(string isn't at least 5 characters in length, then alert("error #1") and return false)
That's pretty much how most of them are, except the following are "else if". And then the final one, if none of them return false, it'll return true which will send them to the database insertion page. The only thing is that I want to do some checking of the database before I allow it to return true. So I've been using the following function to submit the email to see if it already exists, and it checks it fine. But if there are no other problems within the form, it seems to send them to the database insertion page before it gets the chance to validate the emails existence and return an error (if that's the case).
<script type="text/javascript">
$("form").submit(function(){
$.post("resources/scripts/validateDisplayName.php",{ displayName:$("#display-name").attr("value") } ,function(data)
{ if(data=='false') { alert("Display Name Taken. Sorry Buddy."); return false; } });
$.post("resources/scripts/validateEmail.php",{ email:$("#email").attr("value") } ,function(data)
{ if(data=='false') { alert("Email Taken. Sorry Buddy."); return false; } });
if($("#email").attr("value").length<"5") { alert("Your email address must be at least 5 characters long."); return false; }
else if($("#email").attr("value").indexOf("@")<"1") { alert("Your email address is invalid."); return false; }
else if($("#email").attr("value").indexOf(".")<"2") { alert("Your email address is invalid."); return false; }
else if($("#email").attr("value")!=$("#confirm-email").attr("value")) { alert("Your email addresses did not match."); return false; }
else if($("#password").attr("value").length<"6") { alert("Your password must be at least 6 characters long."); return false; }
else if($("#password").attr("value")!=$("#confirm-password").attr("value")) { alert("Your passwords did not match."); return false; }
else if($("#display-name").attr("value").length<"4") { alert("Your display name must be at least 4 characters long."); return false; }
else if($("#code").attr("value")!="4") { alert("Are you sure dogs have that many legs?"); return false; }
else {
return true;
}
});
</script>
Since the PHP validation pages are almost identical, I'll just post one of them.
<?php
$mysql["connection"] = mysql_connect("localhost","root","");
if(!$mysql["connection"]) { die("false"); }
mysql_select_db("smurfworks_net",$mysql["connection"]);
$query = mysql_query("SELECT * FROM `accounts` WHERE email='$_POST[email]'");
if(mysql_num_rows($query)=="0") { die("true"); } else { die("false"); }
?>
If anyone has a solution to make sure the php files return true of false, before the submission goes through the list of ifs and elses, it'd be much appreciated. I have a feeling I could be structuring this wrong all together, but that's something I need to learn.
Thanks in advance!
I've recently adapted to the jQuery library to start making my websites more dynamic, and so far it's gone really well, and I've even started validating forms using jQuery, but I can't seem to get my head around the server side functions in relation to the jQuery ajax functions.
Alright so my problem is that I have a form, and on submission it validates all of the fields using client side methods such as javascript's length() object. I'm using the basic "if, if else, else" methods to work through the list of things to validate and that seems to work well.
if(string isn't at least 5 characters in length, then alert("error #1") and return false)
That's pretty much how most of them are, except the following are "else if". And then the final one, if none of them return false, it'll return true which will send them to the database insertion page. The only thing is that I want to do some checking of the database before I allow it to return true. So I've been using the following function to submit the email to see if it already exists, and it checks it fine. But if there are no other problems within the form, it seems to send them to the database insertion page before it gets the chance to validate the emails existence and return an error (if that's the case).
<script type="text/javascript">
$("form").submit(function(){
$.post("resources/scripts/validateDisplayName.php",{ displayName:$("#display-name").attr("value") } ,function(data)
{ if(data=='false') { alert("Display Name Taken. Sorry Buddy."); return false; } });
$.post("resources/scripts/validateEmail.php",{ email:$("#email").attr("value") } ,function(data)
{ if(data=='false') { alert("Email Taken. Sorry Buddy."); return false; } });
if($("#email").attr("value").length<"5") { alert("Your email address must be at least 5 characters long."); return false; }
else if($("#email").attr("value").indexOf("@")<"1") { alert("Your email address is invalid."); return false; }
else if($("#email").attr("value").indexOf(".")<"2") { alert("Your email address is invalid."); return false; }
else if($("#email").attr("value")!=$("#confirm-email").attr("value")) { alert("Your email addresses did not match."); return false; }
else if($("#password").attr("value").length<"6") { alert("Your password must be at least 6 characters long."); return false; }
else if($("#password").attr("value")!=$("#confirm-password").attr("value")) { alert("Your passwords did not match."); return false; }
else if($("#display-name").attr("value").length<"4") { alert("Your display name must be at least 4 characters long."); return false; }
else if($("#code").attr("value")!="4") { alert("Are you sure dogs have that many legs?"); return false; }
else {
return true;
}
});
</script>
Since the PHP validation pages are almost identical, I'll just post one of them.
<?php
$mysql["connection"] = mysql_connect("localhost","root","");
if(!$mysql["connection"]) { die("false"); }
mysql_select_db("smurfworks_net",$mysql["connection"]);
$query = mysql_query("SELECT * FROM `accounts` WHERE email='$_POST[email]'");
if(mysql_num_rows($query)=="0") { die("true"); } else { die("false"); }
?>
If anyone has a solution to make sure the php files return true of false, before the submission goes through the list of ifs and elses, it'd be much appreciated. I have a feeling I could be structuring this wrong all together, but that's something I need to learn.
Thanks in advance!