JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2009, 09:03 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
Load Text file and display in table format.

I wish to display the output in a nice table format, the text file contains loads of rows and columns and each column separates either by space " " or "," .
Please can someone help, at the moment it says, Problem retrieving data. Unknown

Further I am not sure if i need a webserver to make this work, as currenlty all html files are stored on a network drive and everything works fine except this section.

Code:
<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('T1').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}
</script>
</head>

<body onload="loadXMLDoc('testdetails1.txt')">
<div id="T1" style="border:1px solid black;width:300;padding:5"></div><br />
<button onclick="loadXMLDoc('testdetails2.txt')">Click</button>
<button onclick="loadXMLDoc('testdetails3.csv')">Click</button>

</body>

Reply With Quote
  #2  
Old November 5th, 2009, 09:25 AM
haydenchambers haydenchambers is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2009
Location: Madrid
Posts: 601 haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 5 Days 3 h 2 m 3 sec
Reputation Power: 350
Send a message via Google Talk to haydenchambers Send a message via Skype to haydenchambers
stupid question but the text document is in the same folder (at the same level) as your html doc?
__________________
working on goggle wave... it's like google wave but there's lots more beer involved

Reply With Quote
  #3  
Old November 5th, 2009, 09:39 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
Hello, Text file is in the same folder as the html files, but not sure why i am getting the Unknown error when i click the button.

Reply With Quote
  #4  
Old November 5th, 2009, 09:49 AM
haydenchambers haydenchambers is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2009
Location: Madrid
Posts: 601 haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 5 Days 3 h 2 m 3 sec
Reputation Power: 350
Send a message via Google Talk to haydenchambers Send a message via Skype to haydenchambers
how are you accessing the html?... (I've run into that annoying security issue about "js trying to access local files" a couple of times)

Reply With Quote
  #5  
Old November 5th, 2009, 09:54 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
Just through the webbrowser ie6..

i.e, H:\Web Project\HTML Templates\firstpresentation\errorlogsfinal.html

Reply With Quote
  #6  
Old November 5th, 2009, 09:56 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,839 KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 13 m 27 sec
Reputation Power: 342
Send a message via Yahoo to KorRedDevil
AJAX requests object works only on a server-side level, not locally.
__________________
HELP SAVE ANA

Reply With Quote
  #7  
Old November 5th, 2009, 10:02 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
If we agree what you say, than how come the response of ajax falling under the loop and alert message is popping up:

if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('T1').innerHTML=xmlhttp.responseText;
}
else
{
alert("Problem retrieving data:" + xmlhttp.statusText);
}
}

Reply With Quote
  #8  
Old November 5th, 2009, 10:15 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,839 KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 13 m 27 sec
Reputation Power: 342
Send a message via Yahoo to KorRedDevil
I bet the message tells you that the requested document's status is 4xx (error status), not 200.


Definition:
"With AJAX, a JavaScript can communicate directly with the server, with the XMLHttpRequest object. With this object, a JavaScript can trade data with a web server, without reloading the page.

AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, allowing web pages to request small bits of information from the server instead of whole pages.
"

Reply With Quote
  #9  
Old November 5th, 2009, 10:36 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
Seems quite interesting, but unfortunatley it's not, please if possible i wish you could try on a stand alone pc without the webserver.

Reply With Quote
  #10  
Old November 5th, 2009, 11:32 AM
haydenchambers haydenchambers is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2009
Location: Madrid
Posts: 601 haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level)haydenchambers User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 5 Days 3 h 2 m 3 sec
Reputation Power: 350
Send a message via Google Talk to haydenchambers Send a message via Skype to haydenchambers
what korRedDevil is saying is that ajax calls ONLY work with a web server.. and trying it on a stand alone pc will ALWAYS fail.

Reply With Quote
  #11  
Old November 5th, 2009, 11:55 AM
s-p-n's Avatar
s-p-n s-p-n is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 162 s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level)s-p-n User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 13 h 48 m
Reputation Power: 154
Quote:
Originally Posted by haydenchambers
what korRedDevil is saying is that ajax calls ONLY work with a web server.. and trying it on a stand alone pc will ALWAYS fail.


AJAX works great on my stand alone PC.... (With Wamp Server )

To the OP, if you don't have a host to test it, get Wamp Server. That'll really help!
__________________
- The Wise Guy

Reply With Quote
  #12  
Old November 6th, 2009, 03:43 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,839 KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 13 m 27 sec
Reputation Power: 342
Send a message via Yahoo to KorRedDevil
Quote:
Originally Posted by s-p-n
AJAX works great on my stand alone PC.... (With Wamp Server )

To the OP, if you don't have a host to test it, get Wamp Server. That'll really help!

Maybe I was not very clear. AJAX can not work on a stand alone PC workstation. Nor a classical submit action will work on a stand alone PC. Or AJAX is nothing but a technique to perform a submit action, asynchronously yes, but still a submit action.

But AJAX will work if you install on your PC a web server (like Apache, as I said, or, if you want, any WAMP). When launching the Apache, your PC become a server and it is not "stand alone".

Nor a classical submit action will work on a stand alone PC. Or AJAX is nothing but a technique to perform a submit action, asynchronously yes, but still a submit action.

But even so, AJAX can not perform a request on the network drives, as those drives do not belong to the web server structure.

To bypass that you need to insert a new PC into your local network, to install a web server software on it (and afferent programs your all your network's workstations) which will make it an intranet server. Now your files which get the request must be placed on that server.

Last edited by KorRedDevil : November 6th, 2009 at 03:48 AM.

Reply With Quote
  #13  
Old November 6th, 2009, 03:55 AM
murklys murklys is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 352 murklys User rank is Sergeant (500 - 2000 Reputation Level)murklys User rank is Sergeant (500 - 2000 Reputation Level)murklys User rank is Sergeant (500 - 2000 Reputation Level)murklys User rank is Sergeant (500 - 2000 Reputation Level)murklys User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 15 h 12 m 36 sec
Reputation Power: 21
Basically, to put it in even simpler terms: if you double-click on your html file and expect AJAX to work - it NEVER WILL. If you type an address that starts with http:// to access your html file - it will work. As KorRedDevil said, if you want to be able to use ajax on your PC, install a local web server. You will still be able to access/edit your files like you do now, but they will have to be located in a special folder and you will view them via browser by typing something like http://localhost/test.html.

Reply With Quote
  #14  
Old November 6th, 2009, 04:28 AM
salmansohail salmansohail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 17 salmansohail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 44 m 5 sec
Reputation Power: 0
Million thanks, I managed to get a webserver, but the problem is I got a text file ('testdetails2.txt') and my text file looks something like this:

abc123, 234 pqr,xyz "type"
bc123, 234 pqr,xyz "type"
c123, 234 pqr,xyz "type"
n23, 234 pqr,xyz "type"
aj23, 234 pqr,xyz "type"

As you notice, it has got "," aswell a blank space and "". all i wish to to do is read the the contents of the text file in a nice html table.

I knew I need somekind of javascript funciton to read the text file and convert it into columns, rows and in HTML TABLE. but here i am struggling.

Code:
<script type="text/javascript">   
function loadTxtFile(){.... }
</script> 
<body>   <div id="T1" style="border:1px solid black;width:300;padding:5"></div><br />   <button onclick="loadXMLDoc('testdetails2.txt')">Click</button> </html><html>
</head>   
<script type="text/javascript">   
<body>   
<div id="T1" style="border:1px solid black;width:300;padding:5"></div><br />   
<button onclick="loadTxtDoc('testdetails2.txt')">Click</button>   
<button onclick="loadTxtDoc('testdetails3.csv')">Click</button>   
 </body> 
</html>


I found somekind of function to read and split the text file but, its not working numbers of columns are differ from file to file and so the number of rows

Code:
function table_maker(columns,array){
var txt="<TABLE cellpadding=1 cellspacing=1 border=1>"
var rows=Math.ceil(array.length/columns);
for(var r=0,t=0;r<rows;r++){
txt+="<TR>"
for(var c=0;c<columns;c++){
txt+="<TD>"+array[t++]+"</TD>"
}
txt+="</TR>"
}
txt+="</TR></TABLE>"
return txt;
}

Reply With Quote
  #15  
Old November 6th, 2009, 05:06 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,839 KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level)KorRedDevil User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 13 m 27 sec
Reputation Power: 342
Send a message via Yahoo to KorRedDevil
If you your request returns something like:
Code:
var txt='abc123, 234 pqr,xyz "type"\nbc123, 234 pqr,xyz "type"\nc123, 234 pqr,xyz "type"\nn23, 234 pqr,xyz "type"\naj23, 234 pqr,xyz "type"';

The function could be something like that:
Code:
function table_maker(txt){
var rows=txt.split('\n'), r, cells, c, j, i=0;
var table='<table>';
while(r=rows[i++]){
	table+='<tr>';
	cells=r.split(',');
	j=0;
		while(c=cells[j++]){
		table+=('<td>'+c+'</td>');
		}
	table+='</tr>';
}
table+='</table>';
return table;
}

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Load Text file and display in table format.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek