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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:22:46+00:00 2026-06-12T11:22:46+00:00

I have a small form which contains a first name, last name and a

  • 0

I have a small form which contains a first name, last name and a date. On clicking to submit the form I want it to check the database for a duplicate entry (with Ajax), and if there is already 1+ entries, present a confirm window confirming another submission. The confirm shouldn’t show if there aren’t any entries.

For some reason it seems to be presenting the confirm without the result from the Ajax PHP page. If I introduce an alert after the xmlHttp.send(null) line, it gets the text from the PHP (as wanted), making me think I misunderstand the order the code is executed. Here is the code:

Javascript:

    function check_duplicates() {
        var first = document.getElementById('first_name').value;
        var last = document.getElementById('last_name').value;
        var date = document.getElementById('event_date').value;

        var xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert ("Your browser does not support AJAX!");
            return;
        }

        var result = "ERROR - Ajax did not load properly";
        var url="check_duplicate.php";
        url=url+"?first="+first;
        url=url+"&last="+last;
        url=url+"&date="+date;

        xmlHttp.onreadystatechange=function() {
            if(xmlHttp.readyState==4) {
                result = xmlHttp.responseText;
                alert("RESULT="+result);
                if(result != "clean") {
                    var validate = confirm(result);
                    return validate;
                }
            }
        }

        xmlHttp.open("GET",url,true);
        var test = xmlHttp.send(null);
    }
    function GetXmlHttpObject() {
        var xmlHttp = null;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e) {
                // Internet Explorer
                try {
                    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
        }

        return xmlHttp;
    }

PHP:

// DATABASE CONNECTION INFORMATION REMOVED

$first = $_GET['first'];
$last = $_GET['last'];
$date = date('Y-m-d',strtotime($_GET['date']));

$sql = "SELECT COUNT(*) AS count FROM Table WHERE First='$first' AND ".
        "Last='$last' AND Date='$date'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

if($row['count'] > 0) {
   if($row['count'] == 1) {
      echo "There is already an entry for ".$first." ".$last." on ".
           date('M jS',strtotime($date)).".\n".
           "Are you sure you want to submit this entry?";
   }
   else { // plural version of the same message
      echo "There are already ".$row['count']." entries for ".$first." ".
        $last." on ".date('M jS',strtotime($date)).".\n".
       "Are you sure you want to submit this entry?";
   }
} else {
   echo "clean";
}
  • 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-06-12T11:22:47+00:00Added an answer on June 12, 2026 at 11:22 am

    Here is an answer using synchronous AJAX. This way, you don’t have to overload the default form handling to get it to work. However, all javascript will be blocked while the confirmation request is running, which means your web page may appear to come to a screeching halt for however long the confirmation request lasts.

    This function will return true if the record should be added, and false otherwise.

    function check_duplicates() {
        var first = document.getElementById('first_name').value;
        var last = document.getElementById('last_name').value;
        var date = document.getElementById('event_date').value;
    
        var xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert ("Your browser does not support AJAX!");
            return false;
        }
    
        var result = "ERROR - Ajax did not load properly";
        var url="check_duplicate.php";
        url=url+"?first="+encodeURIComponent(first);
        url=url+"&last="+encodeURIComponent(last);
        url=url+"&date="+encodeURIComponent(date);
    
        xmlHttp.open("GET",url,false);
        xmlHttp.send(null);
    
        var validated = true;
        var result = xmlHttp.responseText;
    
        if (result != 'clean')
            validated = confirm("RESULT="+result);
    
        return validated;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a small form application, which contains textbox only. I have enabled
I have a page with an updatepanel which contains a small login form -
In my c# application i have this small form which is used to set
I'm developing a small POS for a university project. I have a form which
I have a small form that I want to have people fill out on
I have a small form that contains a jQuery UI dialog to ask for
I have a small contact form: <form method=post action=contact.php name=contactform id=contactform> <fieldset> <legend>Please fill
I have a small web form which will cause a PHP script to send
I have a form in a booking system which contains a subform which is
We have an MDI form which contains some number of child forms which have

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.