0

My code is HTML with JavaScript, I can write to a text file using JavaScript (client side) but this is based on 1 item. I want to have multiple forms and don't know the best way to do this? My code is below. What I am effectively trying to do is give the user the ability to write a comment for their policy ref but my coding knowledge isn't very good.

<script language='JavaScript'>
    function WriteToFile() {
        try {
            var t = document.data.comments.value;
            var p = document.getElementById('policynum').innerHTML;
            var msg = p + " | " + t;
            var fso, s;
            fso = new ActiveXObject("Scripting.FileSystemObject");
            s = fso.OpenTextFile("C:\\Test\\Logfile.txt", 8, true);

            s.writeline(msg);
            s.Close();
alert("Comment Saved!");
        }
        catch (err) {
            var strErr = 'Error:';
            strErr += '\nNumber:' + err.number;
            strErr += '\nDescription:' + err.description;
            document.write(strErr);
    }
} 

MY HTML form is below but in reality I will have a list of policy references where the user can enter their comments and the comment will be saved to the text file with the relating policy ref.

    <form action="test.hta" method="post" name="data"> 
<tr align = left><td align = center id = "policynum">A12345678A01</td>
<td align = center id = td1>Testing</td><td align = center id = td1>
<textarea name="comments">this is my comment </textarea><br> 
<input type=button value="Save Comment" OnClick="WriteToFile(this.form)">
</form> 

    <form action="test.hta" method="post" name="data"> 
<tr align = left><td align = center id = "policynum">A12345678A02</td>
<td align = center id = td1>Testing</td><td align = center id = td1>
<textarea name="comments">this is my comment </textarea><br> 
<input type=button value="Save Comment" OnClick="WriteToFile(this.form)">
</form> 

I think I will need to add something so that when OnClick function it relates to the correct form also I think I will need to make sure each form name is different?

1 Answer 1

1

I may be wrong but I thing you want to save a comment and the string fom you td#policynum right?

If so the only mistake is you html. I tried the following with IE11 and it worked:

<form action="test.hta" method="post" name="data"> 
    <table>
        <tr align="left"></td>
            <td align="center" id="policynum">A12345678A01</td>
            <td align="center" id="td1">Testing</td>
            <td align="center" id="td1"></td>
        </tr>
    </table>
    <textarea name="comments">this is my comment</textarea><br> 
    <input type="button" value="Save Comment" OnClick="WriteToFile(this.form)">
</form>

Update

After you comment I guess I know what's wrong with your code. I thought you just have this one form you posted in your question. However...

In case you browser supports querySelector it will be pretty easy:

<form action="test.hta" method="post" name="data"> 
    <table>
        <tr align="left"></td>
            <td align="center" name="policynum">A12345678A01 B1</td>
            <td align="center" id="td1">Testing</td>
            <td align="center" id="td1"></td>
        </tr>
    </table>
    <textarea name="comments">this is my comment</textarea><br> 
    <input type="button" value="Save Comment" OnClick="WriteToFile(this.form)">
</form>

So you can dynamically read the policynum by name:

function WriteToFile(f) { //added the form you clicked from
    try {
        var t = f.comments.value;
        var p = f.querySelector('[name="policynum"]').innerHTML; //readout the policynum by name tag
        var msg = p + " | " + t;
        var fso, s;
        fso = new ActiveXObject("Scripting.FileSystemObject");
        s = fso.OpenTextFile("C:\\Test\\Logfile.txt", 8, true);

        s.writeline(msg);
        s.Close();
        alert("Comment Saved!");
    }
    catch (err) {
        var strErr = 'Error:';
        strErr += '\nNumber:' + err.number;
        strErr += '\nDescription:' + err.description;
        document.write(strErr);
    }
}

Just by the way: you're using some ids multiple times. That's not allowed.

The id attribute provides a document-wide unique identifier for an element.

See: wikipedia html attributes

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Werner but when I add the other policy refs in I get the JavaScript Error: Number:-2146823281 Description:Unable to get property 'value' of undefined or null reference. I think I need set a variable so when a comment is submitted it relates to the correct form.
Could you just add some other refs in your example?
I have just tried your latest code but it looks like my browser does not support querySelector (using IE10) as the policy and comment is not in the table anymore. apologies if I have missed something? thanks
I forgot to comment out this: readout the policynum by name tag. Try it again. I updated the code
Since using your latest code I am now getting an error and the data does not appear inside the table. error code is Description:Unable to get property 'innerHTML' of undefined or null reference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.