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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:39:11+00:00 2026-05-28T13:39:11+00:00

I am trying to send a AJAX request to the server when a user

  • 0

I am trying to send a AJAX request to the server when a user submits a form, I am doing this to check out if that email is already in use by someone else or if it is new.

This is my javascript AJAX function

function check_entireform()
{ 
    var new_email = document.getElementById('jemail').value;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var y =xmlhttp.responseText;
            if(y=="user with that email is already registered")
                return false;
        }
    }

    var x ="user_exists.php?email="+ new_email;
    xmlhttp.open("GET",x,true);
    xmlhttp.send();
}

This is my html input tag

<form method="post" action="./new.php" onsubmit="return check_entireform()">
<input type="text" name="email" id="jemail"/>
</form>

This is my user_exists.php file

   <?php
     $email=$_GET["email"];
     if(mysql_query("SELECT email FROM user_info WHERE email='$email'"))
       echo "user with that email is already registered";
     else
       echo"user got a new email";
   ?>

When the user submits the form, I would like to send the user email to the user_exists.php file through the AJAX and then check out if that email is already in use by someone and echo back some information if it already in use.

I don’t know where it’s going wrong. I have been submiting same email but it does not echo and does not stop me from submitting the form.

If there are any better solutions to find out if email is already in use by someone would be great.Thanks

  • 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-28T13:39:12+00:00Added an answer on May 28, 2026 at 1:39 pm

    The first A in AJAX is for Asynchronous.

    It means that the callback function (the one you defined for onreadystatechange) will only run when the server responds. On the other hand, your submit handler check_entireform will return much earlier than that. It returns undefined, which evaluates to false, so your form is not submitted.

    Whatever you want to do, you should do in the callback function.

    Always prevent submission and pass reference to form:

    <form method="post" action="./new.php" onsubmit="check_entireform(this); return false;">
    

    Accept the reference:

    function check_entireform(theform) {
    

    Then submit the form if needed:

       if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
            var y =xmlhttp.responseText;
            if(y != "user with that email is already registered")
                theform.submit();
       }
    

    jsFiddle Demo – without AJAX, only simulation


    Return something more logical from your PHP

    Tip: Instead of returning a long string from PHP, you should simply return 0 or 1. You should think of the AJAX query like a Yes-No question in this case (Can this email be used?). 1 will mean Yes while 0 will mean No.

    So you could change your code to:

            var canBeUsed =xmlhttp.responseText;
            if (canBeUsed) theform.submit();
    

    It will become much easier to read and maintain.


    URL-encode query string parameter values

    In this line:

     var x ="user_exists.php?email="+ new_email;
    

    You should URL encode the new_email variable, because certain characters (like &) have a special meaning in the query string and shall be encoded. You can use the encodeURIComponent() function to do this:

     var x ="user_exists.php?email="+ encodeURIComponent(new_email);
    

    Sanitize input in your PHP

    ALWAYS sanitize (and validate) any data that comes from the outside. In its current form, your code is vulnerable to SQL Injection, which is a HUGE problem.

    Before inserting data into your query, you should use mysql_real_escape_string() on it (or even better, use PDO for your database stuff).

     $email = mysql_real_escape_string($_GET["email"]);
     if(mysql_query("SELECT email FROM user_info WHERE email='$email'"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a form to send an prototype ajax request to
When a user clicks a link, I would like to send an AJAX request
I have some web application that uses jQuery to send AJAX requests to server.
I am trying to send an ajax request immediately after a date is chosen
Is it possible to send an ajax request in onsubmit() of a form? I
I've been trying to make an AJAX request to an external server. I've learned
I am trying to use jquery's ajax $.get(...) function to send a request to
I'm trying to send a File to the Server without success. I have this
I'm trying to send the node instance JSON data using an ajax get request
I'm trying to use jQuery's .AJAX function to send a request to IPINFODB's API

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.