using
- jquery validator plugin
- jquery
try to:
- make sure a username doesn't exist in the database
- have a form + a field + remote check
- i want to let jquery check using ajax if it exists.
what works:
- check-username.php does work by itself
what doesn't
- form doesn't seem to be able to pass return boolean value to validate the remote jquery validation
- how do I get it to pass the value so that the field can be validated?
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js" type="text/javascript"></script>
<script language="javascript" src="validate.js"></script>
<meta name="generator" content="" />
<style type="text/css"></style>
</head>
<body>
<form action="/" method="post" id="register-form">
<div>
<label for="username">Username<font color="red">*</font>:</label>
<input type="text" name="username" size="20" id="username" />
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
</body>
</html>
JQUERY Validate
$().ready(function() {
// validate signup form on keyup and submit
$("#register-form").validate({
rules: {
username: {
required: true,
remote: "check-username.php"
},
},
messages: {
username:{
remote: "This username is already taken! Try another."
},
},
});
});
Check-USERNAME.php
<?php
$searchVal = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$data", $username, $password);
$sql = "SELECT * FROM users WHERE USERNAME = " . "'" . $searchVal . "'";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result>0){
return true;
}else {
return false;
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
$dbh = null;some rows up.return. Btw, I'm using this - very simple code for checking used names, etc.