[jQuery] on form submission, how to decide "on submit: return true / false"
Hi there,
I got helps and also debugging on self for days on trying to implement
a form submission with jQuery,
now I was stunk on getting the results from call back .Post or .ajax
to pass true & false to the form submission.
the code is pretty simple here:
$(document).ready(function()
{
$("#regForm").submit(function()
{
$.post("check.php", { username: $('#username').val(),password: $
('#password').val() }, function(data)
{
$('#errorMessage').html(data);
if (data == "pass" ) return true; else return false;
//check.php will output { "pass", "user name
already token", "input username & password"}
//problem here, the true / false value doesn't
work.
//even if I commented this line and add return
false, the form will still be submitted.
//I got kind suggestions should try replace $.Post
with $.ajax to call here, but don't know how to.
});
});
});
the full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#regForm").submit(function()
{
$.post("check.php", { username: $('#username').val(),password: $
('#password').val() }, function(data)
{
$('#errorMessage').html(data);
if (data == "pass" ) return true; else return false;
});
});
});
</script>
</head>
<body>
<form TARGET="<?php echo "$PHP_SELF"; ?>" method="POST" id="regForm">
Username:<input type="text" name="username" id="username" />
Password:<input type="password" name="password" id="password" />
<INPUT TYPE=HIDDEN NAME="stage" VALUE="go">
<p id="errorMessage" style='color:#f00'>
<input type="submit" name="submit" id="send" value="Sign Up!" />
</form>
<?php
if ($_POST['stage']=='go')
{
// do registration & insert user data
echo "form submitted";
}
//print_r($_POST);
?>
</body>
</html>