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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.