|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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>
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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)
|
|
#5
|
|||
|
|||
|
Just through the webbrowser ie6..
i.e, H:\Web Project\HTML Templates\firstpresentation\errorlogsfinal.html |
|
#6
|
||||
|
||||
|
AJAX requests object works only on a server-side level, not locally.
__________________
HELP SAVE ANA |
|
#7
|
|||
|
|||
|
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); } } |
|
#8
|
||||
|
||||
|
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." |
|
#9
|
|||
|
|||
|
Seems quite interesting, but unfortunatley it's not, please if possible i wish you could try on a stand alone pc without the webserver.
|
|
#10
|
|||
|
|||
|
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.
|
|
#11
|
||||
|
||||
|
Quote:
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
|
|
#12
|
||||
|
||||
|
Quote:
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. |
|
#13
|
|||
|
|||
|
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.
|
|
#14
|
|||
|
|||
|
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;
}
|
|
#15
|
||||
|
||||
|
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;
}
|
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Load Text file and display in table format. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|