9three Posted November 4, 2009 Share Posted November 4, 2009 Hey, I have this function var xmlHttp = GetXmlHttpObject(); function reportPhoto(div, photoID) { var URL = 'ajax/ajax.reportPhoto.php?photoID='+photoID; if (xmlHttp == null) return; xmlHttp.onreadystatechange = stateChanged(div); xmlHttp.open('POST', URL, true); xmlHttp.send(null); } Which initiates these two functions: function GetXmlHttpObject() { if (window.XMLHttpRequest) { //IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { //IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } function stateChanged(div) { if (xmlHttp.readyState == 4) { document.getElementById(div).innerHTML = xmlHttp.responseText; } } Problem is that it doesn't do anything. No errors are thrown. My table in my DB is empty. I'm running on the latest firefox. Not sure what I'm doing wrong? I normally use Jquery but I wanted to switch over to this method instead. EDIT: I changed somethings around and now when I click on an href twice with that "reportPhoto" function it just disappears the div but nothing else happens. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/ Share on other sites More sharing options...
xtopolis Posted November 4, 2009 Share Posted November 4, 2009 It doesn't look like you're sending the post data correctly. You don't send the correct headers. See this page: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950715 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 Ok so now it is sending the data to the database but it's not switching the HTML when its clicked. var xmlHttp = GetXmlHttpObject(); function reportPhoto(div, photoID) { var URL = 'ajax/ajax.reportPhoto.php'; var params = 'photoID=' + photoID; if (xmlHttp == null) return; xmlHttp.open("POST", URL, true); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Content-length", params.length); xmlHttp.setRequestHeader("Connection", "close"); xmlHttp.onreadystatechange = stateChanged(div); xmlHttp.send(params); } function GetXmlHttpObject() { if (window.XMLHttpRequest) { //IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { //IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } function stateChanged(div) { if (xmlHttp.readyState == 4) { parent.document.getElementById(div).innerHTML = xmlHttp.responseText; } } My PHP file echos out "Picture reported" if true else it prints "Error" Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950737 Share on other sites More sharing options...
xtopolis Posted November 4, 2009 Share Posted November 4, 2009 Debug it step by step. I assume it has something to do with the "parent" reference, but I'm unsure. Start by making the parent.document.getElementById(div).innerHTML = xmlHttp.responseText; line instead say alert(xmlHttp.responseText); and go from there. Test that it alerts the correct data. Then test that you can change a direct reference to the HTML object (document.getElementById('nameofdiv').innerHTML = 'test'). Then test that the parent.document.getElementById(div) is actually getting the right element (and not erring). Then see if the argument div is referencing the object correctly. Try those things, otherwise we may need to see more code. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950739 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 I changed it to what you said. The alert doesn't go through. function stateChanged(div) { if (xmlHttp.readyState == 4) { alert(xmlHttp.responseText); //parent.document.getElementById(div).innerHTML = xmlHttp.responseText; } } If the data went through then the readyState is 4. But it's not prompting the alert box... weird.. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950744 Share on other sites More sharing options...
xtopolis Posted November 4, 2009 Share Posted November 4, 2009 If the data went through then the readyState is 4Not necessarily... This page shows a brief explanation of what codes are returned: http://www.w3schools.com/dom/dom_http.asp If deals with the readystate as well as the page status (which you do not check in your code). Anyway, continue to debug step by step. Place debugging alerts backwards through your code until you find where it actually breaks. ie: function stateChanged(div) { alert('made it to stateChanged()'); if (xmlHttp.readyState == 4) { alert('made it to readystate ==4'); //parent.document.getElementById(div).innerHTML = xmlHttp.responseText; }else{ alert('made it to readystate = ' + xmlHttp.readystate); } } Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950751 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 It's reaching readyState 1 aka Loading. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950755 Share on other sites More sharing options...
xtopolis Posted November 4, 2009 Share Posted November 4, 2009 Well, if it doesn't go past 1, (it should go through them until 4 if it's successful) , then try checking the xmlHttp.status code and see if it is indeed working correctly (200). You need to narrow down the problem since it seems to be between your script and the page it wants to query -- syntactically it seems correct. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950759 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 Hm, not sure if I'm doing it right but I'm getting an error if (xmlHttp.readyState == 4) { alert('made it to readystate ==4'); //parent.document.getElementById(div).innerHTML = xmlHttp.responseText; }else{ alert('made it to readystate = ' + xmlHttp.readyState); alert('status: ' + xmlHttp.statusText); } Tried using xmlHttp.status as well The quey IS being sent though. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950763 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 In my php code it's echoing out "Photo Reported' if all went well. Should I be echoing out "4" for complete? Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950768 Share on other sites More sharing options...
xtopolis Posted November 4, 2009 Share Posted November 4, 2009 The PHP can echo out whatever you want it to. if (xmlHttp.readyState == 4) { alert('made it to readystate ==4'); //parent.document.getElementById(div).innerHTML = xmlHttp.responseText; }else{ alert('made it to readystate = ' + xmlHttp.readyState); alert('status: ' + xmlHttp.statusText); } That code should echo "made it to readystate ==4" if that part of the request made it... Keep trying, I won't be able to assist more tonight, but I'm sure you can figure it out by checking it step by step. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-950783 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 I tried: function stateChanged(div) { if (xmlHttp.readyState == 4) { alert('made it to readystate ==4'); //parent.document.getElementById(div).innerHTML = xmlHttp.responseText; }else{ alert('made it to readystate = ' + xmlHttp.readyState); try { alert('status: ' + xmlHttp.statusText); } catch (e) { alert(e.description); } } } Gives me undefined :-/ Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951013 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Share Posted November 4, 2009 use jquery please Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951041 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 no. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951048 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Share Posted November 4, 2009 no. why not ? scuuuurd ? $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); http://docs.jquery.com/Ajax/jQuery.ajax#options Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951056 Share on other sites More sharing options...
9three Posted November 4, 2009 Author Share Posted November 4, 2009 Stop derailing the thread please. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951057 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Share Posted November 4, 2009 Stop derailing the thread please. its my thread now hahahahah Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951063 Share on other sites More sharing options...
xtopolis Posted November 5, 2009 Share Posted November 5, 2009 You can try posting your full code and we can see if we can pinpoint the problem. Quote Link to comment https://forums.phpfreaks.com/topic/180209-simple-ajax-function/#findComment-951471 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.