Ayon Posted February 9, 2010 Share Posted February 9, 2010 I've just started working with AJAX, and I'm completely stuck on this... So hopefully someone's able to help me out Here's the AJAX code function rate(type) { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } url="rel_rating.php?type=" + type; xmlhttp.open("GET",url,false); xmlhttp.send(null); document.getElementById('rategood').innerHTML=xmlhttp.responseText; } here's where i'm trying to output the result: <span id="rategood">{$relRateGood}</span> <a href="#" onClick="rating('good')">{html_image file='images/thumb_up.png' border='0'}</a> <span id="ratebad">{$relRateBad}</span> <a href="#" onClick="rating('bad')">{html_image file='images/thumb_down.png' border='0'}</a> the "rel_rating.php" only contains this <?php echo $_GET['type']; ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/ Share on other sites More sharing options...
RussellReal Posted February 9, 2010 Share Posted February 9, 2010 instead of : document.getElementById('rategood').innerHTML=xmlhttp.responseText; try: alert(xmlhttp.responseText); Quote Link to comment https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/#findComment-1009319 Share on other sites More sharing options...
seventheyejosh Posted February 9, 2010 Share Posted February 9, 2010 Just do it with jquery. <script type="text/javascript" src="/your/jquery.js"></script> function rate(type){ $("#rategood").load('rel_rating.php?type='+type); }//end function I know it is generally frowned upon to respond to questions with just use 'X' but I really feel that the jquery <edit> '_AJAX_' </edit>library is the only thing anyone should ever use.. if u need more help, or fine tuned control, I'd look at the $.ajax() function, the load() function simply gets html from a file and puts it in the target container. I mean, it is just so nice the way jquery handles the XHR object creation for you. that way you dont have like 50 extra lines of code, and crazy named objects... I just find it to be simpler and less error prone. Quote Link to comment https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/#findComment-1009485 Share on other sites More sharing options...
Ang3l0fDeath Posted February 12, 2010 Share Posted February 12, 2010 @Ayon you missing the onreadystatchange code along with the readystate/status code which lets you know when you request is a) complete b) successful. function rate(type){ if (window.XMLHttpRequest){mlhttp=new XMLHttpRequest();} else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4){ if (xmlhttp.status==200){ document.getElementById('rategood').innerHTML=xmlhttp.responseText;} else{alert('Request Complete - Status Not Successful');}} url="rel_rating.php?type=" + type; xmlhttp.open("GET",url,false); xmlhttp.send(null);} @seventheyejosh if there weren't people who rather do things themselves and learn those things no one could of made ur life easier by giving you the short-cut jquery. Quote Link to comment https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/#findComment-1011430 Share on other sites More sharing options...
RussellReal Posted February 12, 2010 Share Posted February 12, 2010 @Ayon you missing the onreadystatchange code along with the readystate/status code which lets you know when you request is a) complete b) successful. function rate(type){ if (window.XMLHttpRequest){mlhttp=new XMLHttpRequest();} else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4){ if (xmlhttp.status==200){ document.getElementById('rategood').innerHTML=xmlhttp.responseText;} else{alert('Request Complete - Status Not Successful');}} url="rel_rating.php?type=" + type; xmlhttp.open("GET",url,false); xmlhttp.send(null);} @seventheyejosh if there weren't people who rather do things themselves and learn those things no one could of made ur life easier by giving you the short-cut jquery. his javascipt is not asynchronous meaning it will hang until it gets a response or fails Quote Link to comment https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/#findComment-1011468 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.