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

10-01-2008, 03: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 version="1.0" encoding="utf-8"?>
<heading>Quotes about Life</heading>
<text>If we could see the miracle of a single flower clearly, our whole life would change.</text>
<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>
<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>
<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>
<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>
<person>Mark Twain</person>
<text>Let us so live that when we come to die even the undertaker will be sorry.</text>
<person>Oscar Wilde</person>
<text>To live is the rarest thing in the world. Most people exist, that is all.</text>

JavaScript File:

var request;

request = new XMLHttpRequest();
request = new ActiveXObject("MSXml.XMLHTTP");
request = new ActiveXObject("Microsoft.XMLHTTP");
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
case 200:
//---------//////////////// DISPLAY QUOTES \\\\\\\\\\\\\\\\\\--------------
var text = request.responseXML.getElementsByTagName("text")


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

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


Please any help would mean a lot to me.

- Matt.

11-19-2008, 12:45 PM
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

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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum