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):
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.
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.
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>
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.)