Jump to content

I seriously cant get this to work


Ayon

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/191434-i-seriously-cant-get-this-to-work/
Share on other sites

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.

@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.

@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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.