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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:07:38+00:00 2026-05-23T08:07:38+00:00

Okay, so I have a form that a user will fill out, these are

  • 0

Okay, so I have a form that a user will fill out, these are the username field, and a color verification field. The username field automatically checks my database when the focus is changed to alert the user whether they’re username is taken or not. The color verification field compares the color of the image displayed on screen to the color the user enters. If both of these fields are completed successfully, the button to register is displayed. This is the jQuery I am using

$(document).ready(function()
{
    $("#username").blur(function()
    {
    //remove all the class add the messagebox classes and start fading
    $("#msgbox2").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
    //check the username exists or not from ajax
    $.post("checkusername.php",{ username:$(this).val() } ,function(data)
    {
      if(data=='no') //if username not avaiable
      { 
        $("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
        {//alert(data);
          //add message and change the class of the box and start fading
          $(this).html('Your username was taken<? $_SESSION['test2'] = 0; ?>').addClass('messageboxerror') .fadeTo(900,1);
        });     
      }
      else 
      {
        $("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
        { //alert(data);
          //add message and change the class of the box and start fading
          $(this).html('User name is available!<? $_SESSION['test2'] = 1; ?>').addClass('messageboxok').fadeTo(900,1);  
        });


      }

    });

});
});

The php file for the above code is:

session_start();
require("../db.php");
$username = $_POST['username'];

$sql ="SELECT * FROM user WHERE username = '$username'";
$go = mysql_query($sql,$db);
$numrows = mysql_num_rows($go);


if ($numrows) {
// no
echo("no");
} else {
// yes
echo "yes";
}

The second field for color is set up the same:

$(document).ready(function()
{
$("#color").blur(function()
{
    //remove all the class add the messagebox classes and start fading
    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
    //check the color is right or not
    $.post("checkcolor.php",{ color:$(this).val() } ,function(data)
    {
      if(data=='no') //if color is incorrect
      { 
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
        {//alert(data);
          //add message and change the class of the box and start fading
          $(this).html('Your color was incorrect<? $_SESSION['test1'] = 0; ?>').addClass('messageboxerror') .fadeTo(900,1);
        });     
      }
      else 
      {
        $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
        { //alert(data);
          //add message and change the class of the box and start fading
          $(this).html('Verification Successful<? $_SESSION['test1'] = 1; ?>').addClass('messageboxok').fadeTo(900,1);  
        });
      }

    });

});
});

The php file for the above code is:

session_start();

$sescolor= $_SESSION['color'];
$usercolor = $_POST['color'];
$_SESSION['usercolor']= $_POST['color'];
$usercolor = strtoupper($usercolor);
if ($sescolor==$usercolor) {
    echo("yes");
} else {
  echo "no";
}

What I wanted to happen was for the two fields “username” and “color” to change the session variables of ‘test1’ and ‘test2’ based on they’re state. So if username was invalid, test2 would be assigned a 0 instead of 1. Same for the color verification. Then the idea was that I would have a seperate function check to see if both ‘test1’ and ‘test2’ were 1. If so, the user would see the submit button, if not, they would see an error message.

This is what the “checkboth.php” would look like

$test1= $_SESSION['test1'];
$test2= $_SESSION['test2'];


echo $test1;
echo $test2;
if(($test1 ==1) && ($test2 ==1))
{
echo("yes");
}
else{
echo("no");
}

I am using the same jQuery structure for the third function. This works, however, I always get back ‘yes’. Upon a var_dump of the session, I find test1 and test2 to always equal 0. How do I go about accomplishing this?
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-23T08:07:39+00:00Added an answer on May 23, 2026 at 8:07 am

    I prefer ajax to post….after all, you’re trying to do an asynchronous check of data without interrupting the UI experience. That’s the “A” in ajax.

    $.ajax({
    url: "checkusername.php",                   //
    timeout: 30000,
    type: "POST",
    data: data,
    dataType: 'json',
    error: function(XMLHttpRequest, textStatus, errorThrown)  {
        alert("An error has occurred making the request: " + errorThrown)
    },
    success: function(data){
            //do highlights
    }
    });
    

    If you plan on using JSON as return data as I have shown, then you have to return json in your php doc. Right now, you’ve got it returning yes/no printed on the page, which would work if you specified your callback type as text. Otherwise, using your method it’s just going to get confused.

    As to your color verification, I’d “stack” it in the success of the first as you can’t have it happen until after the username is checked.

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

Sidebar

Related Questions

Okay all, I have a form that I want users to fill out. When
Okay, so I have this form that is set to preload a DB record
Okay, here is my problem: I have a form that requires to have two
Okay we have a single - sign - on and the user will likely
I have a form that displays a password field and when the form is
Okay, I have this textfield form that contains some text. What I need to
Okay I have a large CRUD app that uses tabs with Forms embedded in
I have a form that submits through Ajax, which works perfectly at the moment.
I do have an html form that accepts textbox input of sha1 hashes per
Okay so here is my database: I have a page on my .net form

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.