AJAX script not executing PHP file

Discussion in 'JavaScript' started by Bostad, Oct 3, 2011.

  1. #1
    Hi;

    I've been working on this for hours and its driving me nuts.
    I have an AJAX function that, for some reason is not executing a PHP script and I can't figure out why.

    The entire function is pasted below. I have deliberately placed bad syntax at the beginning of the php file so that I will see if it executes.
    If I use form action technique to trigger the php bit, it works, however I want the information to come back into the current page.

    I hit the "script is about to run" alert everytime. If I copy and paste the url from the Firefox console, the php throws the expected error at the right point, so its not a URL problem.

    Any help would be appreciated;

    
    script type="text/javascript" >
    function SearchCode()
    {
    
    var stocode = sCode.STOCode.value;
    var cernercode = sCode.CernerCode.value;
    var stable = sCode.SearchTable.value;
    var xmlhttp;
    if (stable =="" &&
         stocode == "" &&
         cernercode == "")
      {
      alert("Can't enter a blank code");
      document.getElementById("sTarget").innerHTML="";
      return;
      }
    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)
        {
        alert('writing results');
        document.getElementById("sTarget").innerHTML=xmlhttp.responseText;
        }
      }
    alert('script about to run');
    xmlhttp.open("POST","./PHP/LWMSearch.php",true);
    xmlhttp.send();
    }
    </script>
    
    
    
    
    
    
    Code (markup):
     
    Bostad, Oct 3, 2011 IP
  2. Foxtr0t

    Foxtr0t Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I'd use some wrapper library instead of using xmlhttp directly.
     
    Foxtr0t, Oct 3, 2011 IP
  3. Bostad

    Bostad Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm not sure what wrapper libraries are out there, or how it would help. Could you amplify?

    thanks
     
    Bostad, Oct 4, 2011 IP
  4. Bostad

    Bostad Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It seems that it won't run in asynchronous mode. When I set the flag to false, the PHP script runs.
     
    Bostad, Oct 4, 2011 IP
  5. Foxtr0t

    Foxtr0t Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Here's an example list:
    http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript

    As to how it would help, the libraries do low-level stuff for you. In the code snippet above, you deal with such stuff directly, for example you need to think about different browsers.
     
    Foxtr0t, Oct 4, 2011 IP
  6. KsNitro

    KsNitro Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #6
    I would second the use of a framework to do this. It will simplify the process of what you are trying to do. If You're not familiar with frameworks, they are basically libraries of code built on top of javascript. They simplify things such as ajax by giving you useful functions to do routine tasks.

    Have a look at jQuery if you aren't sure where to start.
     
    KsNitro, Oct 4, 2011 IP
  7. developer.designer

    developer.designer Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    script type="text/javascript" >
    function SearchCode()
    {

    var stocode = sCode.STOCode.value;
    var cernercode = sCode.CernerCode.value;
    var stable = sCode.SearchTable.value;
    var xmlhttp;
    if (stable =="" &&
    stocode == "" &&
    cernercode == "")
    {
    alert("Can't enter a blank code");
    document.getElementById("sTarget").innerHTML="";
    return;
    }
    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)
    {
    alert('writing results');
    document.getElementById("sTarget").innerHTML=xmlhttp.responseText;
    }
    }
    alert('script about to run');
    xmlhttp.open("POST","./PHP/LWMSearch.php",true);
    xmlhttp.send();
    }
    </script>
     
    developer.designer, Oct 8, 2011 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    Look into jQuery. The ajax() function is very lightweight (you can do your code in about 3 lines) and it works. (The library itself isn't lightweight, but if you use the Google or Microsoft links [or both - I use MS, with a fallback to Google, with a fallback to a local file - in the probably impossible event that both MS and Google are down but my site is up], the user will be using a cached copy, since so many sites use one or the other for jQuery.)
     
    Rukbat, Oct 8, 2011 IP