0

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.

3 Answers 3

2

Change some of your else ifs into ifs. Using just else if, as soon as a condition matches the rest are skipped. So when you have an invalid username, your code never checks to see if the email is valid or not.

Only use else if when you want ONLY ONE of the outputs.

Sign up to request clarification or add additional context in comments.

Comments

1
$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!';
}

1 Comment

OK! is never actually printed. And when changing 1 field all the error appear? How can I just show the one I want? Using array index?
1

What you would want to do is, instead of elses, just ifs like:

 if(!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i",$username)){

        echo "not a valid username.";
    }
 if(filter_var($email,FILTER_VALIDATE_EMAIL)){

        echo "OK!";
 }
 if(!filter_var($email,FILTER_VALIDATE_EMAIL)){

    echo "not a valid email address";
 }
 if(strcmp($email,$email2) != 0){

     echo "emails are different.";
 }
 etc....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.