SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax script doesn't always work...

    I've got an ajax script that doesn't always work... The PHP part works because I've tested it so it must have something do do with the ajax...

    HTML Code:
    <input type="text" name="company" id="company" style="width: 98%;" value="<?php echo $info[company]; ?>" onblur="checkLeadDuplicates('company',this)" />
    
    <div id="lead_company" style="visibility: hidden;"></div>
    Code:
    var AJAXForms = false;
    var LastField = null;
    var isIE = false;
    // on !IE we only have to initialize it once
    if (window.XMLHttpRequest) {
        AJAXForms = new XMLHttpRequest();
    }
    function checkLeadDuplicates(fieldname,field) {
        if (window.XMLHttpRequest) {
            // branch for IE/Windows ActiveX version
        } else if (window.ActiveXObject) {
            AJAXForms = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        AJAXForms.onreadystatechange = processLeadDuplicates(fieldname);
        AJAXForms.open("GET", "modules/leads/ajax-duplicates.php?op=ajax&field="+fieldname+"&value="+field.value);
        AJAXForms.send(null);
    }
    function processLeadDuplicates(fieldname) {
        if (AJAXForms.readyState == 4) {
            var res = document.getElementById("lead_"+fieldname);
            res.innerHTML = AJAXForms.responseText;
            res.style.visibility = "visible";
        }
    }
    Can you see anything wrong? I tried using onchange on the input but that didn't work...

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the Firebug extension for Firefox to see the headers being sent and the response being received by your Ajax calls.

  3. #3
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found my answer...

    Instead of

    Code:
    AJAXForms.onreadystatechange = processLeadDuplicates(fieldname);
    I used

    Code:
    AJAXForms.onreadystatechange = function() { 
            if (AJAXForms.readyState == 4) {    
                var res = document.getElementById("codecheck");
                res.innerHTML = AJAXForms.responseText;
                res.style.visibility = "visible";    
            }
        };

  4. #4
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohh I am struggling for the same.
    Thanx Mr. Tech for posting the solution,too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •