473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

problems reading and writing to and from files via ajax on Mac

I'm using ajax to read and write to a text file. The code works on Windows, but not in Macintosh. Any suggestions on what is wrong with the code? Thanks much-- a friend wrote me this code, and I don't really understand how it works!

This part of the code appears in the html file, and calls the php file fileReader.php:

Expand|Select|Wrap|Line Numbers
  1. function getContents()
  2. {
  3. url = "fileReader.php";
  4. var browserName = navigator.appName;
  5. //for Moz, FF, NN, Op
  6. if (browserName.indexOf("Microsoft") == -1)
  7.     {
  8.               reqXML = new XMLHttpRequest();            
  9.             }
  10. //IE
  11. else if (browserName.indexOf("Microsoft") != -1) 
  12.     {
  13.               reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
  14.             }
  15. if(reqXML)
  16.     { 
  17.                reqXML.onreadystatechange = function ()
  18.         {
  19.         if(reqXML.readyState == 4){
  20.             }
  21.             }
  22.     reqXML.open("GET", url, true);         
  23.     reqXML.send(null);                     
  24.     }   
  25.     return reqXML.responseText;       
  26. }
  27.  

This part of the code appears in the fileReader.php file:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $myFile = "data.txt";
  4. $fh = fopen($myFile, 'r') or die("can't open file");
  5. $theData = fread($fh, filesize($myFile));
  6. fclose($fh);
  7.  
  8. echo $theData;
  9. ?>
  10.  
This part of the code appears in the html file, and calls the php file fileWriter.php:

Expand|Select|Wrap|Line Numbers
  1. function ajaxCall(response)
  2. {
  3. url = "fileWriter.php?update=" + response;
  4. var browserName = navigator.appName;
  5. //for Moz, FF, NN, Op
  6. if (browserName.indexOf("Microsoft") == -1) 
  7.     {
  8.     reqXML = new XMLHttpRequest();           
  9.     reqXML.open("GET", url, true); 
  10.               reqXML.send(null);                        
  11.             }
  12. //IE
  13. else if (browserName.indexOf("Microsoft") != -1)
  14.     {
  15.               reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
  16.              if(reqXML)
  17.         { 
  18.                   reqXML.open("GET", url, true);         
  19.                    reqXML.send(null);    
  20.             }
  21. }
  22.  
Finally, this part of the code appears in the fileWriter.php file:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $update = $_GET['update'];
  4.  
  5. $myFile = "data.txt";
  6. $fh = fopen($myFile, 'a') or die("can't open file");
  7.  
  8. fwrite($fh, $update);
  9.  
  10. ?>
  11.  
Aug 2 '07 #1
5 1624
pbmods
5,821 Expert 4TB
Heya, katiebeals. Welcome to TSDN!

I believe [code=javascript] is what you are looking for.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Aug 2 '07 #2
Thanks for your reply.

GetContents() isupposed to return a string that consists of the contents of the file data.txt.

ajaxCall(response) is supposed to append the value of the variable response (as string variable) to the file data.txt.

Both js routines [code=js] call the php routines [code=php] I've included in my original post.
Aug 3 '07 #3
acoder
16,027 Expert Mod 8TB
Thanks for your reply.

GetContents() isupposed to return a string that consists of the contents of the file data.txt.

ajaxCall(response) is supposed to append the value of the variable response (as string variable) to the file data.txt.

Both js routines [code=js] call the php routines [code=php] I've included in my original post.
For writing, you should use POST rather than GET because it makes a modification and the variable response could be long. See an example of a POST request.
Aug 3 '07 #4
Thanks. I tried changing 'GET' to 'POST' but still no luck.

I now think the problem is that the responseText method is returning a null value in Safari. Does anyone know a way around this?
Aug 15 '07 #5
pbmods
5,821 Expert 4TB
Heya, Katie.

Try print_r()ing your response to see what's in it.
Aug 15 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: yawnmoth | last post by:
i'm trying to read / write a file, and am having some problems... here's the php script i'm trying to read / write with: <? $fp = fopen("test","r+"); while (!feof($fp)) { $temp=fgets($fp);...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
5
by: A. Gaubatz | last post by:
In VB6, I would open files with: Open <file name> For <input/output/append> As <file number> I could then use the Write/Input commands to modify/read the file. How do I do this in VB.NET? ...
8
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
5
by: RyanL | last post by:
I'm a newbie with a large number of data files in multiple directories. I want to uncompress, read, and copy the contents of each file into one master data file. The code below seems to be doing...
2
by: sandipm | last post by:
Hi, I am trying to read a file and write into other file. if I do it for simple text file, it works well. but for pdfs or some other mime types, its failing. actually main problem is i am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.