I'm sending some data by Ajax to a PHP script, which handles registration. I'm trying to get a response from the server whether an input is valid or not (live validation). I want each input to have its own response, that means if the first input is still invalid and someone puts something invalid in the second input field, the first response stays and it also sends the second response.
What now happens is this: First input is invalid, I see the response, but when I go the next input field and put something invalid in the field, the first response just stays there.(I have tested this with console.log in Chrome)
UPDATE: to give an example, I'm seeing this: not a valid username!
and then I put some invalid email address in the next field and I still see not a valid username!
.
This is my PHP code:
if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['email2']) && isset($_POST['password'])
&& isset($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['gender']) && isset($_POST['day'])
&& isset($_POST['month']) && isset($_POST['year']) ) {
$username = $_POST['username'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$gender = $_POST['gender'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
if(!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i",$username)){
echo "not a valid username.";
}
else if(filter_var($email,FILTER_VALIDATE_EMAIL)){
echo "OK!";
}
else if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
echo "not a valid email address";
}
else if(strcmp($email,$email2) != 0){
echo "emails are different.";
}
else if(strcmp($email,$email2) == 0){
echo "OK!";
}
else if(!preg_match("[a-zA-Z]*",$firstname)){
echo "Not a valid firstname.";
}
else if(preg_match("[a-zA-Z]*",$firstname)){
echo "OK!";
}
else if(!preg_match("[a-zA-Z]*",$surname)){
echo "not a valid surname.";
}
else if(preg_match("[a-zA-Z]*",$surname)){
echo "OK!";
}
}
and this is the JQuery Ajax code:
function handlePost() {
var username = $('#username').val();
var email = $('#email').val();
var email2 = $('#email2').val();
var password = $('#password').val();
var firstname = $('#firstname').val();
var surname = $('#surname').val();
var gender = $('#gender').val();
var day = $('#day').val();
var month = convertMonth($('#month').val())
var year = $('#year').val();
$.ajax({
type: "POST",
url: "handleRegister.php",
data: "username="+username+"&email="+email+"&email2="+email2+"&password="+password+"&firstname="
+firstname+"&surname="+surname+"&gender="+gender+"&day="+day+"&month="+month+"&year="+year,
success: function(resp){
// we have the response
//alert("Server said:\n '" + resp + "'");
console.log("Server said:\n '" + resp + "'")
},
error: function(e){
//alert('Error: ' + e);
console.log("Server said:\n '" + e + "'")
}
});
}
I would think it's the way I'm using if/else here. Also I'm a little confused about how/when to use isset($_POST['submit']) in this case?
Thanks in advance.
Answer
$errors = array();
if (!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i", $username))
{
$errors[] = "not a valid username.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = "not a valid email address";
}
if ($email !== $email2)
{
$errors[] = "emails are different";
}
if (!preg_match("[a-zA-Z]*", $firstname))
{
$errors[] = "Not a valid firstname.";
}
if (!preg_match("[a-zA-Z]*", $surname))
{
$errors[] = "not a valid surname.";
}
if ($errors)
{
echo implode("\n", $errors);
}
else
{
echo 'OK!';
}
No comments:
Post a Comment