c_pattle Posted July 23, 2010 Share Posted July 23, 2010 I'm just starting to use ajax and have the following code. All it does is call the function updateForm when I change text in a text box and the end result should be an alert box saying "server is done". However I keep getting the error message saying that "request is undefined". However I made an alert box print out the contents of requestion and it came up with "[object XMLHttpRequest]" so I don't understand why. <script language="javascript" type="text/javascript"> function createRequest() { var request; try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; } } } if (request) alert(request); if (!request) alert("Error initializing XMLHttpRequest!"); } function updateForm() { createRequest(); var company = document.getElementById("company_name").value; var url = "ajaxformupdate.php?company=" + escape(company); request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); } function updatePage() { alert("Server is done!"); } </script> Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted July 23, 2010 Share Posted July 23, 2010 Because you are not defining 'request' in the scope of the calling function. Here is the change function updateForm() { var request = createRequest(); var company = document.getElementById("company_name").value; var url = "ajaxformupdate.php?company=" + escape(company); request.onreadystatechange = function () { if (req.readyState==4) { if (req.status==200) { alert('Server is done!'); } } }; request.open("GET", url, true); request.send(null); } 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.