...

View Full Version : How to call 1 text line with ajax/javascript?



skillionaire
10-01-2008, 02:23 AM
Hey fellas,

I have a question in which I'm in dire need of help. I hit a brick wall and do NOT know how to move forward.

I have a XML file and a HTML file with JavaScript in it. Inside my xml are 10 or so lines to text in between tags to call each line. What I need to do is upon clicking a button in HTML I need to generate 1 line of text of that XML.

So every time I hit that button a new line of text will appear and the old one will disappear.

So my question is how do I overcome this? Any help/advice would be tremendously appreciate here.

Here's my code so far:

XML FILE:


<?xml version="1.0" encoding="utf-8"?>
<quotes>
<heading>Quotes about Life</heading>
<quote>
<person>Buddha</person>
<text>If we could see the miracle of a single flower clearly, our whole life would change.</text>
</quote>
<quote>
<person>Carl Jung</person>
<text>There are as many nights as days, and the one is just as long as the other in the year's course. Even a happy life cannot be without a measure of darkness, and the word 'happy' would lose its meaning if it were not balanced by sadness.</text>
</quote>
<quote>
<person>Helen Keller</person>
<text>Security is mostly a superstition. It does not exist in nature, nor do the children of men as a whole experience it. Avoiding danger is no safer in the long run than outright exposure. Life is either a daring adventure or nothing.</text>
</quote>
<quote>
<person>Henry James</person>
<text>Be not afraid of life. Believe that life is worth living, and your belief will help create the fact.</text>
</quote>
<quote>
<person>Martin Luther King Jr.</person>
<text>An individual has not started living until he can rise above the narrow confines of his individualistic concerns to the broader concerns of all humanity.</text>
</quote>
<quote>
<person>Mark Twain</person>
<text>Let us so live that when we come to die even the undertaker will be sorry.</text>
</quote>
<quote>
<person>Oscar Wilde</person>
<text>To live is the rarest thing in the world. Most people exist, that is all.</text>
</quote>
</quotes>


JavaScript File:


var request;

try{
request = new XMLHttpRequest();
}catch(e){
try{
request = new ActiveXObject("MSXml.XMLHTTP");
}catch(e1){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e2){
request = null;
}
}
}


window.onload = function(){


request.open('POST', 'sample_quotes.xml',true);


request.onreadystatechange = function(){

//4 being all loaded
if(request.readyState == 4){
//A file has loaded
switch(request.status){
case 200:
//ok
//alert('OK.');
//---------//////////////// DISPLAY QUOTES \\\\\\\\\\\\\\\\\\--------------
var text = request.responseXML.getElementsByTagName("text")


for(i=0;i<text.length;i++){

var p = document.createElement("p");
var t = document.createTextNode(text[i].childNodes[0].nodeValue);
p.appendChild(t);
document.getElementById('MyID').appendChild(p);
}
//---------//////////////// DISPLAY QUOTES ENDS \\\\\\\\\\\\\\\\\\--------------


break;
case 404:
//File not found
alert('Sorry. My dog ate the file.');
break;
case 403:
//Permission Denied
alert("Sorry. My mom won't let me give you the file");
break;
case 500:
//Server Side error
alert("Sorry. PHP feel asleep while building your file.");
break;
default:
alert("I don't know what happened. But it wasn't good. lol");
}
}
}

request.send(null);
}

Please any help would mean a lot to me.
Thanks

- Matt.

v08i
11-19-2008, 11:45 AM
what I can suggest is,

1- you keep all the text nodes in an array
2- take a var myVar =0
3- display myVar - th element [0th currently] of your array
4- whenever someone clicks on the button in your form
5- increment myVar by 1
6- display myVar - th element [1st currently] of your array

- now if value of myVar equals the length of array; reset it to 0 and start displaying again


thanks
vijayjoshi.org (http://vijayjoshi.org)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum