i'm having some issues getting a modal form validate a username from Mysql database using PHP.
here is my PHP script to validate the username, when i run it alone it works but when it will not get called from the remote validator.
PHP code :
checkUsername.php
<?php
$isAvailable = true;
//get the username and password
$uname = trim($_POST['username']);
$umail = trim($_POST['email']);
//connect to database
require_once '/php-includes/dbconfig.inc.php';
$stmt = $DB_con->prepare("SELECT username, email FROM member WHERE username=:uname OR email=:umail");
$stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['username']==$uname) {
$isAvailable = false;
}
// Finally, return a JSON
echo json_encode(array('valid' => $isAvailable));
?>
and this is the formValidation.io script that i'm using from http://ift.tt/1OFKcKf
$(document).ready(function() {
$('#registerForm')
.formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
userName: {
validators: {
notEmpty: {
message: 'The user name is required'
},
remote: {
url: 'checkUsername.php'
}
}
}
}
})
// This event will be triggered when the field passes given validator
.on('success.validator.fv', function(e, data) {
// data.field --> The field name
// data.element --> The field element
// data.result --> The result returned by the validator
// data.validator --> The validator name
if (data.field === 'userName'
&& data.validator === 'remote'
&& (data.result.available === false || data.result.available === 'false'))
{
// The userName field passes the remote validator
data.element // Get the field element
.closest('.form-group') // Get the field parent
// Add has-warning class
.removeClass('has-success')
.addClass('has-warning')
// Show message
.find('small[data-fv-validator="remote"][data-fv-for="userName"]')
.show();
}
})
// This event will be triggered when the field doesn't pass given validator
.on('err.validator.fv', function(e, data) {
// We need to remove has-warning class
// when the field doesn't pass any validator
if (data.field === 'userName') {
data.element
.closest('.form-group')
.removeClass('has-warning');
}
});
});
</script>
Aucun commentaire:
Enregistrer un commentaire