Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:10:18+00:00 2026-05-30T22:10:18+00:00

I’m sending some data by Ajax to a PHP script, which handles registration. I’m

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:10:19+00:00Added an answer on May 30, 2026 at 10:10 pm
    $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!';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.