Ajax Problems

P

PFA\(.\)\(.\)Coder

I am trying to use ajax to create a voting system. I used the tutorial by
Rasmus found here:
http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html

It's pretty much a cut-n-paste and the method updateCount() works great, no
issues.
(See script below)

I made an identical method, updateMana, and cannot get it to work.

In the updateMana method, the alert() shows that the correct info is being
sent to mana.php.
Browsing the mana.php page directly will output the correct data, in the
correct format.
But the browser reports an error:
Line: 57
char: 5
Object required
Code: 0

Line 57 is : document.getElementById(mana[0]).style.width = mana[1]+"px";

The report div is at top of page, and when the script is executed, I can see
it drop the other elements down, but there is no text in the box at all.

My first attempt with Ajax, hopefully someone can help me out!!
--Ridge



the script:
<script language="JavaScript" type="text/javascript">
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function updateCount(action){
http.open('get', 'ninja.php?link_id='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}

//Begin my methods

function updateMana(action,vari,session_id) {
http.open('get',
'mana.php?site='+escape(action+'&vote='+vari+'&session='+session_id));
http.onreadystatechange = handleMana;
http.send(null);
alert(action + " - " + vari + " - " + session_id); //Show what is being
sent, it is correct!!!
}

function handleMana() {
if(http.readyState == 4){
var myresponse = http.responseText;
var mana = new Array();
if(myresponse.indexOf('|' != -1)) {
mana = myresponse.split('|');
if(mana[1] == "Voted!"){
document.getElementById("report").innerHTML = 'You have already voted
today!';
}else{
document.getElementById("report").innerHTML = myresponse; //Trying to
see what is returned, its EMPTY, not undefined
document.getElementById(mana[0]).style.width = mana[1]+"px";
}
}
}
}
</script>
 
P

PFA\(.\)\(.\)Coder

Ian Collins said:
Line 57 is : document.getElementById(mana[0]).style.width = mana[1]+"px";
There isn't an element with an id of whatever is in mana[0]. Stick in an
alert and see what it is.

Thanks Ian, but I have checked the mana page manually and it is outputting
the correct ID reference, and the element does exist in the html. I did put
the output of mana in an alert, and it popped up and empty box.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top