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

  • Home
  • SEARCH
  • 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 7010189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:57:33+00:00 2026-05-27T21:57:33+00:00

I wrote some ajax code that sends values to a php file for validation

  • 0

I wrote some ajax code that sends values to a php file for validation but I have a problem with it. This is my ajax code:

<html>
<body>
<style>
#displayDiv{
background-color: #ffeeaa;
width: 200;
}
</style>
<script type="text/javascript">
function ajaxFunction(str)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged()
{
if(httpxml.readyState==4)
{
document.getElementById("displayDiv").innerHTML=httpxml.responseText;

}
}
var url="username_exists.php?username=";
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>
</head>
<body>
<input type="text"
onkeyup="ajaxFunction(this.value);" name="username" /><div id="displayDiv"></div>
</body>
</html>

and the above code send values to username_exists.php :

<?php
$username = trim($_REQUEST['username']);

if (usernameExists($username)) {
  echo '<span style="color:red";>Username Taken</span>';
}
else {
  echo '<span style="color:green;">Username Available</span>';
}

function usernameExists($input) {
  // in production use we would look up the username in
  // our database, but for this example, we'll just check
  // to see if its in an array of preset usernames.
  $name_array =  array ('k','steve', 'jon', 'george', 'admin');

  if (in_array($input, $name_array)) {
    return true;
  }
  else {
    return false;
  }
}
?>

My problem is that when any username is put in the textbox returns “Username Available”! What did I do wrong?

  • 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-27T21:57:34+00:00Added an answer on May 27, 2026 at 9:57 pm

    You are sending an empty string as username to check thru your ajax call.

    var url="username_exists.php?username=";
    

    I would suggest you to start using jQuery to help you in handling this case.You don’t need to worry about the cross browser issue and you dont need to write all these lines of code.

    jQuery is a wonderful javascript library. http://www.jquery.com

    Include jquery library (i will use the CDN Version) in your head section of HTML document and change your code like this.

    HTML

    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
    </head>
    <body>
    <input type="text" id=txtUserName"  />
    </body>
    

    javascript

      $("#txtUserName").keyUp(function(){
       if($("#txtUserName").val()!="")
       {
          $.ajax({
            url: 'username_exist.php?username'+$("#txtUserName").val(),
            success: function(data) {
                    alert(data);
            }
          });
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a jQuery function that will send to a php file some
Alright, so I have some jQuery code that will send an AJAX request to
I have some very simple sample code like this: $.ajax({ url: 'demo2.htm', success: function(loadeddata){
I am making some consecutives(recrusive) ajax request to a php file that writes request
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some CSS in my HTML code to create rollover buttons. Then i
I wrote some code to read a file in my Java Servlet class. (I'm
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
So i have all these links in html like this <a href=#ajax class=invlink competition_id=532>Gen
I made a website that depends on PHP, AJAX and Javascript. The problem here

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.