Form + write to file

35 views
Skip to first unread message

MonaA

unread,
Jun 8, 2011, 9:31:01 AM6/8/11
to phonegap
Hi guys,

So I finally got the writer and reader thing working in my iPad app.
However, I want the user to fill out a form and submit the content of
the form and place it in a text file. Then I would read the text file
and parse the information line by line and populate the information. I
can't seem to get the form information and write it to a file though.
Am I missing something?

Here is my code for javascript and HTML

<script type="text/javascript" charset="utf-8" src="phonegap.
0.9.5.1.min.js"></script>
function onLoad() {
document.addEventListener("deviceready", onDeviceReady,
false);
}

// PhoneGap is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS,
fail);
}

function gotFS(fileSystem) {
fileSystem.root.getFile("info_account.txt",
{create:true,exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};

writer.write(document.forms.info[0].value+"\n");
writer.write(document.forms.info[1].value+"\n");
writer.write(document.forms.info[2].value+"\n");
writer.write(document.forms.info[3].value+"\n");
}


function fail(evt) {
console.log(evt.target.error.code);
}

</script>

<body onload="onLoad()">

<form action="account.html" method="post" name="info" id="info">
<h1>Account Manager</h1>
<table>

<tr>
<td>Practice / Company Name:
<input type="text" name="practicename" id="practicename" value=""></
td>
</tr>

<tr>
<td>First Name:
<input type="text" name="firstname" id="firstname" value=""></td>
</tr>

<tr>
<td>Last Name:
<input type="text" name="lastname" id="lastname" value=""></td>
</tr>

<tr>
<td>Email:
<input type="text" name="email" id="email" value=""></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</table>
</form>

</body>

Nick McCloud

unread,
Jun 8, 2011, 11:53:35 AM6/8/11
to phonegap
This bit: document.forms.info[0].value is wrong.

Try: document.forms[0]. practicename.value to get the practice name.

Also, I'd try using native local storage for html5 as it would be a
much simpler way to hold settings data.

MonaA

unread,
Jun 8, 2011, 12:07:16 PM6/8/11
to phonegap
I think the document.forms.info[0].value works fine. I alerted it and
the content from the form gets displayed properly. I tried your way
and nothing showed up. So I am not sure. Also, this local storage
thing didn't work. I got an example from the web and nothing
displayed. Does it work in phonegap? Have you ever tried it?

Thanks

MonaA

unread,
Jun 8, 2011, 12:44:52 PM6/8/11
to phonegap
Never mind. I found another way and it finally works.. :) Thanks!
Reply all
Reply to author
Forward
0 new messages