Home/Support/Support Forum/Javascript and textfiles
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Javascript and textfiles

0 votes

Im busy with a project that uses Ajax and a Digi connect me. In my project i read out textfiles with ajax and send the data to the screen.(already have this working nicely) But now i also want JavaScript to give some data back to the Digi.
(im using net os 7.4) at the moment im kinda stuck with it. Any help will be really nice.

Can i use JavaScript to write data to a text-file on the server of is there is much better way?

already thank for reading this and i hope someone can help me

yours sincerely,

asked Jun 29, 2010 in NET+OS by Timmie New to the Community (16 points)
recategorized Dec 10, 2013 by tuxembb

Please log in or register to answer this question.

6 Answers

0 votes
Not 100% certain, but I think you have to emulate a POST operation when using XMLHttpRequest().
This is trickier than it possibly need be using AWS, because you post the data back to mypage_Form_1.htm, for example.
Two suggestions to try:
a) Set up a page containing a textarea to receive the data, and POST data to the textarea; write the file somewhere using code.
b) Set up a form which will accept a file upload, and simulate the upload.

I found this link http://www.yvoschaap.com/weblog/ajax_inline_instant_update_text_20/ which might give some ideas
answered Jun 29, 2010 by steved2 Seasoned Professional (182 points)
0 votes
Yes i looked at the post commands for a few days now. I dont understand this in the ajax example also. If i grab the ajax example
var whereisThecustomer = ""; var postData; var dataType; var theLength; // set up variables for the POST if(whatToPost == "theZipCode") { postData = document.getElementById("theZipCode"); dataType = "theZipCode"; theLength = "theZipCode".length; }
if(request) { //Doing a POST request.open("POST", whereisThecustomer, true); // set up requied fields in the header request.setRequestHeader('Content-type','application/x-www-form-urlencoded'); request.setRequestHeader('Content-length',theLength + postData.length + 1); //We have opened the request request.onreadystatechange = function() { //ready state change occurred if(request.readyState == 4 && request.status == 200) { // if you want to do something on receipt, add code here } } // send out the post request.send(dataType + "=" + postData.value); }

i dont understand the part what happens after the send command. Where is de data giong in this example? Is it giong to a function just a variable? I my example it seems that it is not working at all. (thx for the first reply steved2)
answered Jun 29, 2010 by Timmie New to the Community (16 points)
0 votes
You're basically generating data which is what the browser would generate when doing a 'Submit'. Hence the two lines generating headers.
Variable whereisThecustomer holds the URL which is the target of the POST
answered Jun 30, 2010 by steved2 Seasoned Professional (182 points)
0 votes
first of all thank for the reply again steved2. Your short comment cleared some up for me but the only thing that is still confusing me is the fact that when i press a submit button the browers is making a request and the data is being handled inside de c code of the AJAX_V.c file(the get and set function). But if i am make a request then (like in the ajax example of digi), then they are making a request to the ajax form ,but where is the data being handles inside de c code?? (does is go to some sort of function or variable of something) . Many thank already steved2

your sincerely,

answered Jun 30, 2010 by Timmie New to the Community (16 points)
0 votes
The data is handled exactly as if you had 'submit' the form in the normal way.
So for example, function void setThePhoneNumber(char *theValuePtr) is called when the phone number changes.
With Ajax you only send the value(s) that have changed, instead of the complete form, and I'm guessing that AWS handles this, and only calls each 'set' function when it has an updated value to pass to it.

I've not tried the example, but it rather looks as if the current values are stored in file /FS/RAM0/formsData.txt - and int updateFile(fileFields fieldToUpdate, char * dataToUpdate, int dataLength) puts the data there
answered Jul 1, 2010 by steved2 Seasoned Professional (182 points)
0 votes
if got it working now, many thank for your help steved2.
postData = document.getElementById("theZipCode");
The problem that occurred at the ajax example was that when i got theZipCode by its element id. It didnt really worked correctly because when in the example you wanted to access the value of the item or the lenght something went wrong and it didn't do anything.
request.setRequestHeader('Content-length',theLength + postData.length + 1);
request.send(dataType + "=" + postData.value);
After i worked my way around this it worked perfectly. Again thanks again for explaining it to me and taking the time for it.

(Made the next changes in my example)
postData = document.ajaxForm.theZipCode.value; //and request.send(dataType + "=" + postData);
instead of
postData = document.getElementById("theZipCode"); //and request.send(dataType + "=" + postData.value); [code] now it works perfect[code]
postData = document.getElementById("theZipCode");
request.send(dataType + "=" + postData.value);
now it works perfect[code]

now it works perfect
answered Jul 5, 2010 by Timmie New to the Community (16 points)