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 7677969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:32:22+00:00 2026-05-31T17:32:22+00:00

I want to add a feature to my registration form which I will check

  • 0

I want to add a feature to my registration form which I will check if that username already exists in the database.

I have a few questions about AJAX –

  1. I want to create an AJAX request on_change function, so something like this –

    $('#username').change(function() {
      $.ajax({
      url: "validation.php"
      });
    });
    

    So, as far as I understood, I must have all validations made in PHP inside the validation.php file, correct? Is there any special validation needed or can it be just simple validation with a sql statement – SELECT * FROM 'users' WHERE 'username' = '. $_POST['username'];

  2. So as I understood I must pass the POST values via $.ajax too, correct? If yes, how will I be able to access them via the validation.php file?

  3. After I get the results in validation.php file, how can I pass them back (true or false — exists or doesn’t exist)? I will need to pass them back, and then do an if check, if it’s true – show an error that the username already exists, otherwise, don’t show anything?

  • 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-31T17:32:23+00:00Added an answer on May 31, 2026 at 5:32 pm

    Before continuing, SELECT * FROM 'users' WHERE 'username' = '. $_POST['username']; is just ASKING for a SQL Injection. I suggest you use PHP Data objects.

    So as I understood I must pass the POST values via $.ajax too, correct? If yes, how I will be able to access them via validation.php file?

    Because this is a simple request, I suggest you use JQuery’s method $.post(). Here’s a sample based off of what you’re trying to do.

    $.post('validation.php',{username: $('#username').val()}, function(data){
        if(data.exists){
            //tell user that the username already exists
        }else{
            //username doesn't exist, do what you need to do
        }
     }, 'JSON');
    

    jQuery’s post method takes 4 parameters $.post(url, data, callback, datatype). In the example above, we will be posting the username with $('#username').val() to validation.php and expect a JSON response. When the request is finished, the callback function will be executed with data being the response from the request. Because we specified that that response will be JSON, we can access it just like a native object in javascript. Now let’s move to validation.php

    Like I stated above, I suggested you use PDO for your database driver. So in this example, I will show you a basic usage of it.

    //set the headers to be a json string
    header('content-type: text/json');
    
    //no need to continue if there is no value in the POST username
    if (!isset($_POST['username'])) {
        exit;
    }
    
    //initialize our PDO class. You will need to replace your database credentials respectively
    $db = new PDO('mysql:host=DATABASE_HOST;dbname=DATABASE_NAME;charset=utf8mb4', 'DATABASE_USERNAME', 'DATABASE_PASSWORD');
    
    //prepare our query.
    $query = $db->prepare('SELECT COUNT(*) FROM users WHERE username = :name');
    //let PDO bind the username into the query, and prevent any SQL injection attempts.
    $query->bindParam(':name', $_POST['username']);
    //execute the query
    $query->execute();
    
    //return the JSON object containing the result of if the username exists or not. The $.post in our jquery will access it.
    echo json_encode(array('exists' => $query->fetchColumn() > 0));
    

    Now to recap, our jQuery script will post to validation.php where it selects a username from the database. It will return a JSON object that has a key of exists that is a boolean indicating if the username already exists as a row in your database. When the request is complete via jQuery, you can do what you need based off the result of the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an already built application and I want to add a feature that
I have an existing webapp and want to add a tag feature so that
i want to add such feature in my Application that App will recognize letters,
I want to add a feature to our application that let's a user know
I want to add a feature to a linux-based web service that allows untrusted
I have a simple jsp/servlet application and I want to add AJAX feature to
i already created a table for comments but i want to add the feature
I have some models witch are using Doctrine nestedset feature. I want to add
currently i have a project using solr,now i want to add some feature,so i'm
I want to add a feature to my iPad app that is similar to

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.