Drezard Posted May 18, 2010 Share Posted May 18, 2010 My basic ajax example. It fails on this line here: httpObject.open("GET", "test.php?keyword=" +document.getElementById('keyword').value, true); Except I'm not sure why. Heres the whole code: <html> <head> <script type="text/javascript"> function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } function doWork(){ httpObject = getHTTPObject(); if (httpObject != null){ httpObject.open("GET", "test.php?keyword=" +document.getElementById('keyword').value, true); alert("sent null?"); httpObject.onreadystatechange = setOutput; } } function setOutput(){ if (httpObject.readyState == 4){ alert("Ready state does equal 4"); document.getElementById('output').innerHTML = httpObject.responseText; } } </script> </head> <body> <form name="keywordform"> Search: <input type='text' name='keyword' onkeyup="doWork();" id ="1" /> </form> <div id='output'>blank</div> </body> </html> Any help? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 18, 2010 Share Posted May 18, 2010 can you be more specific than "it fails" ? Quote Link to comment Share on other sites More sharing options...
Drezard Posted May 18, 2010 Author Share Posted May 18, 2010 It fails on that line. So basically. It loads AJAX. It runs the javascript code (if I put alerts in) up to that line and then does nothing past that line. It doesn't output anything to screen either. It doesn't throw the "sent null?" alert. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 18, 2010 Share Posted May 18, 2010 Which browser are you using. If you're not using Firefox, use it and get the firebug add-on. It's wonderful for debugging AJAX problems. If you want to make you're life much easier when doing AJAX, I recommend that you learn how to use one of the JS Libraries like jQuery. Ken Quote Link to comment Share on other sites More sharing options...
AE117 Posted May 18, 2010 Share Posted May 18, 2010 Here you go I will give you the code first and then explain what I did <html> <head> <script type="text/javascript"> function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } function doWork(value){ httpObject = getHTTPObject(); if (httpObject != null){ httpObject.open("GET", "test.php?keyword="+value, true); alert ("working"); httpObject.onreadystatechange = setOutput; document.getElementById("output").innerHTML = value; } } function setOutput(){ if (httpObject.readyState == 4){ alert("Ready state does equal 4"); document.getElementById('output').innerHTML = httpObject.responseText; } } </script> </head> <body> <form name="keywordform"> Search: <input type='text' name='keyword' onkeyup="doWork(this.value);" id ="1" /> </form> <div id='output'>blank</div> </body> </html> I think what you where going for was to change the bottom area to the text being typed. You dont actaully need to send out a request to a page. So i told the function doWork when called to get this.vale. Which means the element that it is on. If you wanted another element you could call it using the getElementby and so on. The function doWork(value) value is now set to the this.value so it is now like a var there is no real reason to open and document but i left that there just for kicks. As you can see it show working the it gets the document but the id output and changes the innerHtml to the Value which is called in the doWork(value) the rest of the code is useless let me know if this is what you where looking for or if you need more help Quote Link to comment Share on other sites More sharing options...
Drezard Posted May 18, 2010 Author Share Posted May 18, 2010 Here you go I will give you the code first and then explain what I did <html> <head> <script type="text/javascript"> function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } function doWork(value){ httpObject = getHTTPObject(); if (httpObject != null){ httpObject.open("GET", "test.php?keyword="+value, true); alert ("working"); httpObject.onreadystatechange = setOutput; document.getElementById("output").innerHTML = value; } } function setOutput(){ if (httpObject.readyState == 4){ alert("Ready state does equal 4"); document.getElementById('output').innerHTML = httpObject.responseText; } } </script> </head> <body> <form name="keywordform"> Search: <input type='text' name='keyword' onkeyup="doWork(this.value);" id ="1" /> </form> <div id='output'>blank</div> </body> </html> I think what you where going for was to change the bottom area to the text being typed. You dont actaully need to send out a request to a page. So i told the function doWork when called to get this.vale. Which means the element that it is on. If you wanted another element you could call it using the getElementby and so on. The function doWork(value) value is now set to the this.value so it is now like a var there is no real reason to open and document but i left that there just for kicks. As you can see it show working the it gets the document but the id output and changes the innerHtml to the Value which is called in the doWork(value) the rest of the code is useless let me know if this is what you where looking for or if you need more help Thanks heaps for the help. I kind of get whats going on here... Need a bit more reading of basic AJAX maybe. Quote Link to comment Share on other sites More sharing options...
AE117 Posted May 18, 2010 Share Posted May 18, 2010 A great place to start is www.w3schools.com they provide great examples its how i started Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.