Write to a text file using an HTA - hta

I am trying to create an HTA file that allows a user to add data to a text file. I am fairly Inexperienced in the area.
I need to accomplish a few things:
Write the bit of data that is inputted to a text file (which my code does)
Let the user chose from a drop-down list of available files to write to
It would be nice if the data could be submitted onClick or by hitting Enter
Clear the fields on submit
Log every entry. No overwriting of data in the text file (my code overwrites)
example:
123
456
789
I apologize if my code looks choppy.
Any help or advice would be appreciated.
Thank you.
<html><head>
<SCRIPT LANGUAGE="VBScript">
Sub Window_onLoad
window.resizeTo 480,150
End Sub
</SCRIPT>
<script language="javascript">
function Writedata()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var write_id;
write_id = document.getElementById('write_id').value ;
alert('The data has been written to \n' + write_id);
var s = fso.CreateTextFile(write_id, true);
s.WriteLine(document.getElementById('name_id').value);
s.Close();
}
</script>
</head>
<body>
<table>
<tr>
<td>Input: </td>
<td><input type="text" name="name" value="" id="name_id"></td>
<td><select><option name="write" value="C:\temp\test1.txt" id="write_id">Option1</option> </select></td>
<td><input type="button" onclick="Writedata()" value="submit"></td>
</tr>
</table>
</body>
</html>

Looks like you've edited your code. Though it can be more flexible:
var s = fso.CreateTextFile(write_id, true); maybe rather:
var s = fso.OpenTextFile(write_id, 2, true);
where 2 represents a bytecode:
1 = read, 2 = write, 8 = append.
write_Id should contain a full path to file, like D:/Foldername/filename.txt.
That's not how you should use select, rather give the id for select itself like this:
<select id="write_id">
<option value="C:\temp\test1.txt">Option1</option>
</select>
Now the value of selected option is stored also as value of select, which you can read in your script exactly like you are doing now.
Also you can declare a variable and assign a value together in JS:
var write_id = document.getElementById('write_id').value;

Related

How to read checkbox/radio value in freemarker?

I'm working with share forms in alfresco and trying to read the values of ticked checkboxes and checked radio buttons form a form. I extended both the user creation and userprofile form with these input controls and so far I have been unsuccessful at reading the textual values of said controls. Below is a snippet of code:
<div class="row">
<span class="label"><input id="${el}-input-spokenEnglish" type="checkbox" name="spokenLanguages" value="${msg("label.anglais"!"")?html}" /> ${msg("label.anglais")}</span>
<span class="label"><input id="${el}-input-spokenSpanish" type="checkbox" name="spokenLanguages" value="${msg("label.espagnol"!"")?html}" /> ${msg("label.espagnol")}</span>
<span class="label"><input id="${el}-input-spokenGerman" type="checkbox" name="spokenLanguages" value="${msg("label.allemand"!"")?html}" /> ${msg("label.allemand")}</span>
<span class="label"><input id="${el}-input-spokenChinese" type="checkbox" name="spokenLanguages" value="${msg("label.chinois"!"")?html}" /> ${msg("label.chinois")}</span>
<br/>
<span class="label">${msg("label.otherLanguages")} : </span>
<span class="input"><input id="${el}-input-spokenLanguages" type="text" size="30" maxlength="256" value="" <#immutablefield field="spokenLanugages" /> /> </span>
</div>
unfortunately I get nothing so far from whatever is returned and would gladly appreciate some insight into this.fre
If you look at userprofile.get.html.ftl, you'll see the following snippet:
<script type="text/javascript">//<![CDATA[
var userProfile = new Alfresco.UserProfile("${args.htmlid}").setOptions(
{
This means it's triggering a client-side JS file from Alfresco, in this case profile.js (see the head file). So just adding some input fields isn't enough.
You need to extend the client-side JS file.
In the function onEditProfile it gets the Dom elements.
But that's just for showing the actual fiels 'after' it's saved.
In profile.js you'll see: form.setSubmitAsJSON(true); that you have a json object from which you can get your fields.
And in userprofile.post.json.ftl it does a loop on the user.properties:
for (var i=0; i<names.length(); i++)
{
var field = names.get(i);
// look and set simple text input values
var index = field.indexOf("-input-");
if (index != -1)
{
user.properties[field.substring(index + 7)] = json.get(field);
}
// apply person description content field
else if (field.indexOf("-text-biography") != -1)
{
user.properties["persondescription"] = json.get(field);
}
}
user.save();
This probably means that you haven't extended the contentmodel of the cm:person object with your new properties.

Post to current page, keeping querystring values in tact

I have a page that I want to create a drop down list, and post back to the same page that I am on now.
How can I do this?
One catch is, I want all the querystring values to be the same also, except for 1 which is what the drop down list will be overriding.
You can use both querystring and form variables in the same page if you want. If you use <form method="post"> and leave the action empty, it will post the form back to the current page, so that's one problem solved. There is one caveat: I'm not sure if leaving the action empty will keep the querystring parameters intact or not.
If not, you can try something like this: <form method="post" action="index.asp?<%= request.querystring %>"> (not sure about the exact syntax, the gist is that you will need to specify the current page and add the current querystring variables in the method).
In the ASP code on your page after posting you can check both request.form and request.querystring. request.form will contain your form post variables. request.querystring will contain the variables behind the ? in your URL.
HTH,
Erik
A Javascript method:
<html>
<head>
<script>
function jumpto(whatform, querykey) {
//get the url querystring
var url = window.location.search;
//the replace query
var queryrx = new RegExp("([?&])" + querykey + "=[^\&]+(?=&|$)", "gi");
//which item selected in dropdown
var index=whatform.pageselect.selectedIndex;
//if the first option, ignore it since it is blank
if (whatform.pageselect.options[index].value != "0") {
//is a query string available
if (url.length>0) {
//our query key is present
if (queryrx.test(url)) {
//now we replace the querystring from old to new
url = url.replace(queryrx, '$1' + querykey + '='+whatform.pageselect.options[index].value);
//clear out the question mark from the querystring
url = url.replace("?", '');
//our query key is not present, but there is querystring data
}else{
url = url.replace("?", '');
url = querykey + "=" + whatform.pageselect.options[index].value + "&" + url;
}
//no querystring data exists
}else{
url = querykey + "=" + whatform.pageselect.options[index].value;
}
//alert(url);
//this is the url we are getting bounced to
location = "mypage.asp?"+url;
}
}
</script>
</head>
<body>
<FORM NAME="form1">
<SELECT NAME="pageselect" ONCHANGE="jumpto(this.form, 'thequerykey')" SIZE="1">
<OPTION VALUE="">Choose a Page</OPTION>
<OPTION VALUE="pageA">First Page</OPTION>
<OPTION VALUE="pageB">Second Page</OPTION>
</SELECT>
</FORM>
</body>
</html>
If you want to go with an ASP Classic solution, you will need to use a function to clear your old value from your querystring
https://stackoverflow.com/a/1221672/2004151
And then print your querystrings as Hidden Input fields in your form (MyFunctionResultsExceptPageSelect below).
Something like:
<FORM ACTION="mypage.asp" METHOD="GET" NAME="form3">
<%=MyFunctionResultsExceptPageSelect("pageselect")%>
<SELECT NAME="pageselect" ONCHANGE="document.form3.submit()" SIZE="1">
<OPTION VALUE="">Choose a Page</OPTION>
<OPTION VALUE="pageA">First Page</OPTION>
<OPTION VALUE="pageB">Second Page</OPTION>
</SELECT>
</FORM>
<%
Function MyFunctionResultsExceptPageSelect(key)
Dim qs, x
For Each x In Request.QueryString
If x <> key Then
qs = qs & "<input type=""hidden"" name="""&x&""" value="""&Request.QueryString(x)&""" />"
End If
Next
MyFunctionResultsExceptPageSelect = qs
End Function
%>
If you want to get the current page instead of manually specifying it, then use the following.
In javascript snippet, use the answer here:
https://stackoverflow.com/a/5817566/2004151
And in ASP, something like this:
http://classicasp.aspfaq.com/files/directories-fso/how-do-i-get-the-name-of-the-current-url/page.html

check in page customization in UCM -adding check boxes for metadata

I want to allow the user to select multiple options for one metadata,and we need to provide check boxes for the same.I have modified the respective include and added the java script ,but some how the value is not getting assigned to the metadata:
I have modified the include and added JS as follows ,but the metadata is not getting assigned the selected values,can any one tell me where am I going wrong:
<script type="text/javascript">
function getSelected(Language) {
var selected = new Array();
var index = 0; for (var intLoop=0; intLoop < Language.length; intLoop++) {
if (Language[intLoop].selected) { index = selected.length;
selected[index] = new Object;
selected[index].value = Language[intLoop].value;
selected[index].index = intLoop; } }
return selected;
}
function submit(selected)
{
var value =selected;
xTranslateTo = value;
}
</script>
<$if strEquals(fieldName, "xTranslateTo") and not (isInfo or isQuery)$>
<tr <$strTrimWs(inc("std_nameentry_row_attributes"))$>>
<td <$if captionFieldWidth$>width="<$captionFieldWidth$>"<$endif$> <$if isInfo$>align=right<$endif$>><$strTrimWs(inc(fieldCaptionInclude))$></td>
<td <$if isFieldInfoOnly$>colspan="100"<$endif$> <$if captionEntryWidth$>;width="<$captionEntryWidth$>"<$endif$>><$inc(fieldEntryInclude)$>
<INPUT TYPE=CHECKBOX NAME="Language" VALUE="English">English
<INPUT TYPE=CHECKBOX NAME="Language" VALUE="Italian">Italian
<INPUT TYPE=CHECKBOX NAME="Language" VALUE="French">French
<INPUT TYPE=CHECKBOX NAME="Language" VALUE="German" >German</td>
</tr>
<a href="javascript:;" onclick=" getSelected()">
<$else$>
<$include super.std_nameentry_row$>
<$endif$>
Can any one throw some light on where am I going wrong
I suppose your problem is this line:
xTranslateTo = value;
IDOC is running on server side, hence all metadata variables are not accessible in JS on client side. Right now your script is just setting value of the local JS variable with name 'xTranslateTo', nothing else.
You need to create hidden field 'xTranslateTo' and populate it in your JS instead of this local variable. Once this hidden field is sent during submit it will be parsed on the server side and put to the corresponding metafield.
Of course it will also require an additional parsing on IDOC for setting previously selected languages' checkboxes.
I'm able to display check boxes and assign comma separated value to the metadata,now all i'm left to do is to display the checked boxes when the user calls for update metadata form.
<$if strEquals(fieldName, "xLang") and not (isInfo or isQuery)$>
<script>
function boxFunc()
{
var textval=document.getElementsByName('xLang')[0];
var langs=document.getElementsByName("ucmlang");
larray="";
for(i=0;i<langs.length;i++)
{
if(langs[i].checked)
{
larray=larray+langs[i].id+",";
}
}
larray=larray.substring(0,larray.length-1);
textval.value=larray;
}
</script>
<tr <$strTrimWs(inc("std_nameentry_row_attributes"))$>>
<td<$if captionFieldWidth$>width="<$captionFieldWidth$>"<$endif$> <$if isInfo$>align=right<$endif$>><$strTrimWs(inc(fieldCaptionInclude))$></td>
<td <$if isFieldInfoOnly$>colspan="100"<$endif$> <$if captionEntryWidth$>;width="<$captionEntryWidth$>"<$endif$>><$inc(fieldEntryInclude)$></td>
<td><!– addition –>English<!– end addition –><input type="checkbox" name="ucmlang" id = "English" onclick="boxFunc()">
<td><!– addition –>French<!– end addition –><input type="checkbox" name="ucmlang" id = "French" onclick="boxFunc()"></td>
<td><!– addition –>Italian<!– end addition –><input type="checkbox" name="ucmlang" id = "Italian" onclick="boxFunc()"></td>
<td><!– addition –>German<!– end addition –><input type="checkbox" name="ucmlang" id = "German" onclick="boxFunc()"></td>
<td><!– addition –>Japanese<!– end addition –><input type="checkbox" name="ucmlang" id = "Japanese" onclick="boxFunc()"></td>
</tr>
<$else$>
<$include super.std_nameentry_row$>
<$endif$>

Problems while Styling a form element that gets data back with ajax

im facing a strange problem while trying to style a form element. A select element in particular.
the idea is simple. i got one select, and when its state changes, through ajax, a second selects gets its data throung a request. so far, everything is ok. the ajax is working fine. but when the first select changes its state, the select element styling is getting back to normal styling.
a live example can be found here
im using 2 main files, the important content of file 1 file is this
<script language="javascript" type="text/javascript">
/* FUNCTION : Ajax Initialize - Start */
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
/* FUNCTION : Ajax Initialize - End */
/* FUNCTION : Get Values - Start */
function getValue(numb) {
var strURL="find.php?theValue="+numb;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('my_requestDiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
/* FUNCTION : Get Values - End */
</script>
and
<form method="post" action="" name="form1" id="example">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="160" class="title_style">What</td>
<td width="340">
<select class="select" name="start" onChange="getValue(this.value)">
<option value="">please make a selection</option>
<option value="1">Colors</option>
</select>
</td>
</tr>
<tr>
<td width="160" class="title_style">Result</td>
<td width="340">
<div id="my_requestDiv">
<select class="select1" name="my_request" >
<option>Please select above first</option>
</select>
</div>
</td>
</tr>
</table>
</form>
where, after the user makes a selection on select element 1, through ajax, a variable is passed to find.php
<?php
$what=intval($_GET['theValue']);
$link = mysql_connect('localhost', 'YOUR_USERNAME_HERE', 'YOUR_PASSWORD_HERE');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_query("SET NAMES 'utf8'", $link);
mysql_select_db('cry-for-help');
$query="SELECT id, value FROM my_table WHERE requested='$what'";
$result=mysql_query($query);
?>
<select class="select" name="my_request">
<option>make your selection now</option>
<?php
while($row=mysql_fetch_array($result)) {
?>
<option value=<?php echo $row['id']?>><?php echo $row['value']?></option>
<?php
}
?>
</select>
and the result is posted back on file 1 as content for the second select element.
The Problem is that the styling is gone.
you can download the example files here
any help would be appreciated. Also, im sorry if my english is not that good, and my programming skills not that advanced to explain my problem with less words.
Jan
Based on the page to which you linked, it looks the problem is that you are replacing all of the extra HTML elements which have been generated by jqtransform. In your JavaScript callback, you are doing this:
document.getElementById('my_requestDiv').innerHTML=req.responseText;
This replaces the entire contents of the div, blowing away all of the extra elements that jqtransform puts in there. What you actually want to do is replace just the select element itself with the results from your PHP service, or perhaps run jqtransform again after updating the div.
Since you're already including jQuery, I would recommend using their powerful selection functionality to replace just the select element:
$('select[name="my_request"]').replaceWith(req.responseText);
I'm not familiar with the jqtransform library so you may need to investigate further to see which route makes the most sense.

Error occurs when jQuery processing a form is inside another form

I am writing a form using jQuery and encounter some difficulties.
My form works fine in static page (html).
However, when I use the form in dynamic page(aspx), the form does not behave correctly.
I cannot append items to the form and call the form.serialize function.
I think the error occurs when a form is inside another form (.aspx code needs to enclosed by a form tag).
What should I do?
Let me give a simplified version of my code:
<form name="Form1" method="post" id="Form1">
some content
<form name="form_inside">
<input name="fname" type="text" />
</form>
</form>
jQuery code:
$("#form_inside").append($("<input type='text' name='lname'>"));
When the user submits,
$("#form_inside").serialize();
// it should return fname=inputfname&lname=inputlname
I want to append element to "form_inside" and serialize the form "form_inside".
The form "Form1" is required by the aspx and I cannot remove it.
Could you just serialize the fields inside Form1?
I don't know anything about ASP, but it seems that you're not doing a straightforward "submit" anyway - so does it really matter if the fields aren't within their own separate form?
You could possibly group the fields you're interested in within a <div> or something, e.g.:
<div id="my-interesting-fields">
...
</div>
then substitute #form-inside with #my-interesting-fields where appropriate - is that helpful at all?
Edit
OK, a quick glance at the jQuery code suggests that serialize() depends on the form's elements member.
I suppose you could hack this in a couple of different ways:
Copy all elements from #my-interesting-fields into a temporary <form> that you dynamically create outside Form1, then call serialize() on that. Something like:
$("#Form1").after("<form id='tmp-form'></form>").
append("#my-interesting-fields input");
$("tmp-form").serialize();
Or, create an elements member on #my-interesting-fields, e.g.
$("#my-interesting-fields").elements = $("#my-interesting-fields input");
$("#my-interesting-fields").serialize();
I haven't tried either of these, but that might give you a couple of ideas. Not that I would necessarily recommend either of them :)
Because you can't have nested <form> tags you'll need to close off the standard dotnet form tag like below:
<script type="text/javascript">
$(document).ready(function() {
$("#form_inside").append($("<input type='text' name='lname'>"));
$("#submitBtn").click(function() {function() {
var obj = $("#form_inside *");
var values = new Array();
obj.each(function(i,obj1) {
if (obj1.name && !obj1.disabled && obj1.value) {
values.push(obj1);
};
});
alert(jQuery.param(values));
}); });
});
</script>
<form id="form1" runat="server">
<div>
<div id="form_inside" name="form_inside"> <input name="fname" type="text" /><input type="button" id="submitBtn" /></div>
</div>
</form>
jQuery.param on a array of form elements will give you the same results as .serialize()
so you get all elements in div $("#form_inside *) then filter for elements then on the result jQuery.param will give you exactly what you need

Resources