Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Quick question about using a table of text input fields within ajavascrip function (http://www.velocityreviews.com/forums/t939609-quick-question-about-using-a-table-of-text-input-fields-within-ajavascrip-function.html)

Ted Byers 10-07-2009 03:26 PM

Quick question about using a table of text input fields within ajavascrip function
 
Please consider this stripped down perl function that prints a
Javascript function:

sub print_javascripts {
print "<script LANGUAGE=\"Javascript\" type=\"text/javascript
\">var n = 0</script>\";
print "<script LANGUAGE=\"Javascript\" type=\"text/javascript\">
function addRow(id){
n++;\"
var str = 'item' + n;
var tbody = document.getElementById(id).getElementsByTagName
(\"tbody\")[0];
var row = document.createElement(\"tr\");
var input = document.createElement('input');
input.type = 'text';
input.name = 'str';
input.size = 50;
input.maxlength = 256;
var data = document.createElement(\"td\");
data.appendChild(input);
row.appendChild(data);
tbody.appendChild(row);
} </script>";
}

What we have here is a perl function used in a CGI script to print a
Javascript function to standard out.

As you can see, this table will look like an extensible list box,
where the user can add values by clicking on a button (of type
button) . Also, I have a little code to allow me to give each value a
unique name. Of course, the cgi script can reconstruct these names IF
it knows how many values were added.

First question: how can I create, within this function, a hidden field
that it to hold the number of new items added, in the first time it is
invoked, and update it subsequently.

Second question, I wrote this on the assumption that text input fields
have to have unique names. Is this assumption correct, or can I give
all the text input fields in this table the same name, so that in my
perl script I can get all of them as an array (the perl CGI package
allows one to get an array of all values for, e.g. options in a list
box, as an array). A listbox isn't really an option because in my
select box to allow the user to select these values after they have
been added to my DB uses the text entered to display to the user but
the value is a unique ID obtained from the DB, and there is no way to
know what ID the DB will assign to a given item.

Thanks

Ted

Captain Paralytic 10-07-2009 03:36 PM

Re: Quick question about using a table of text input fields within ajavascrip function
 
On 7 Oct, 16:26, Ted Byers <r.ted.by...@gmail.com> wrote:
> Please consider this stripped down perl function that prints a
> Javascript function:
>
> sub print_javascripts {
> * * print "<script LANGUAGE=\"Javascript\" type=\"text/javascript
> \">var n = 0</script>\";
> * * print "<script LANGUAGE=\"Javascript\" type=\"text/javascript\">
> * * * * * function addRow(id){
> * * * * * * *n++;\"
> * * * * * * *var str = 'item' + n;
> * * * * * * *var tbody = document.getElementById(id).getElementsByTagName
> (\"tbody\")[0];
> * * * * * * *var row = document.createElement(\"tr\");
> * * * * * * *var input = document.createElement('input');
> * * * * * * *input.type = 'text';
> * * * * * * *input.name = 'str';
> * * * * * * *input.size = 50;
> * * * * * * *input.maxlength = 256;
> * * * * * * *var data = document.createElement(\"td\");
> * * * * * * *data.appendChild(input);
> * * * * * * *row.appendChild(data);
> * * * * * * *tbody.appendChild(row);
> * * * * * } </script>";
>
> }
>
> What we have here is a perl function used in a CGI script to print a
> Javascript function to standard out.
>
> As you can see, this table will look like an extensible list box,
> where the user can add values by clicking on a button (of type
> button) . *Also, I have a little code to allow me to give each value a
> unique name. *Of course, the cgi script can reconstruct these names IF
> it knows how many values were added.
>
> First question: how can I create, within this function, a hidden field
> that it to hold the number of new items added, in the first time it is
> invoked, and update it subsequently.
>
> Second question, I wrote this on the assumption that text input fields
> have to have unique names. *Is this assumption correct, or can I give
> all the text input fields in this table the same name, so that in my
> perl script I can get all of them as an array (the perl CGI package
> allows one to get an array of all values for, e.g. options in a list
> box, as an array). *A listbox isn't really an option because in my
> select box to allow the user to select these values after they have
> been added to my DB uses the text entered to display to the user but
> the value is a unique ID obtained from the DB, and there is no way to
> know what ID the DB will assign to a given item.
>
> Thanks
>
> Ted


The perl script is of no interest in this. It is either a javascript
question or it is not. If it is, please dump the perl code and just
post javascript and its associated javascript question.

Thomas 'PointedEars' Lahn 10-07-2009 03:48 PM

Re: Quick question about using a table of text input fields within a javascrip function
 
Ted Byers wrote in comp.lang.javascript:
^^^^^^^^^^

> Please consider this stripped down perl function [...]


No.

BTW, your Perl function could use a little polish; for example,
all these escape sequences are not necessary.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Ted Byers 10-07-2009 04:37 PM

Re: Quick question about using a table of text input fields within ajavascrip function
 
On Oct 7, 11:36*am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 7 Oct, 16:26, Ted Byers <r.ted.by...@gmail.com> wrote:
>
>
>
> > Please consider this stripped down perl function that prints a
> > Javascript function:

>
> > sub print_javascripts {
> > * * print "<script LANGUAGE=\"Javascript\" type=\"text/javascript
> > \">var n = 0</script>\";
> > * * print "<script LANGUAGE=\"Javascript\" type=\"text/javascript\">
> > * * * * * function addRow(id){
> > * * * * * * *n++;\"
> > * * * * * * *var str = 'item' + n;
> > * * * * * * *var tbody = document.getElementById(id).getElementsByTagName
> > (\"tbody\")[0];
> > * * * * * * *var row = document.createElement(\"tr\");
> > * * * * * * *var input = document.createElement('input');
> > * * * * * * *input.type = 'text';
> > * * * * * * *input.name = 'str';
> > * * * * * * *input.size = 50;
> > * * * * * * *input.maxlength = 256;
> > * * * * * * *var data = document.createElement(\"td\");
> > * * * * * * *data.appendChild(input);
> > * * * * * * *row.appendChild(data);
> > * * * * * * *tbody.appendChild(row);
> > * * * * * } </script>";

>
> > }

>
> > What we have here is a perl function used in a CGI script to print a
> > Javascript function to standard out.

>
> > As you can see, this table will look like an extensible list box,
> > where the user can add values by clicking on a button (of type
> > button) . *Also, I have a little code to allow me to give each value a
> > unique name. *Of course, the cgi script can reconstruct these names IF
> > it knows how many values were added.

>
> > First question: how can I create, within this function, a hidden field
> > that it to hold the number of new items added, in the first time it is
> > invoked, and update it subsequently.

>
> > Second question, I wrote this on the assumption that text input fields
> > have to have unique names. *Is this assumption correct, or can I give
> > all the text input fields in this table the same name, so that in my
> > perl script I can get all of them as an array (the perl CGI package
> > allows one to get an array of all values for, e.g. options in a list
> > box, as an array). *A listbox isn't really an option because in my
> > select box to allow the user to select these values after they have
> > been added to my DB uses the text entered to display to the user but
> > the value is a unique ID obtained from the DB, and there is no way to
> > know what ID the DB will assign to a given item.

>
> > Thanks

>
> > Ted

>
> The perl script is of no interest in this. It is either a javascript
> question or it is not. If it is, please dump the perl code and just
> post javascript and its associated javascript question.


Your response, and Thomas' response, implies that there is no place to
ask about mixed language programming. I have perl on the server side
and Javascript on the client side. I am sure I'd get a similar
response if I asked in the perl fora. This question involves both
perl and Javascript, but I asked here because it is biased toward
modifying the existing javascript function so that the perl script
knows what data the form is passing to it. If it is really
unacceptable to ask a question about mixed language programming here,
do you at least know of a forum where such questions are accepted?

The heart of this question involves the issue of writing each so they
play nicely together. Creating an extensible table with text input
fields in the table's cells is not that hard, and writing perl script
to get the value for any particular parameter is trivially easy, IF
you know the name of the field at the time you write the code.
Getting the Javascript code that handles the extensible table to work
in a way that plays nicely with the perl code that has to handle it is
not so obvious. With the existing code, new text input fields are
created, so obviously their names and number of them can not be known
at the time the code is written. In this case, the most useful answer
to modifying the javascript function has to be constructed taking into
account knowledge of how the perl will need to process the
parameters. If you don't know how to construct such an answer, there
is no point in replying.

Cheers,

Ted

VK 10-07-2009 06:05 PM

Re: Quick question about using a table of text input fields within ajavascrip function
 
> Your response, and Thomas' response, implies that there is no place to
> ask about mixed language programming. *I have perl on the server side
> and Javascript on the client side. *I am sure I'd get a similar
> response if I asked in the perl fora. *This question involves both
> perl and Javascript, but I asked here because it is biased toward
> modifying the existing javascript function so that the perl script
> knows what data the form is passing to it. *If it is really
> unacceptable to ask a question about mixed language programming here,
> do you at least know of a forum where such questions are accepted?


comp.lang.javascript as one of Big 8 Usenet hierarchy newsgroups has
its Charter defining the purpose of the group and the borders of
allowed topics of discussion. If posting in one of Big 8 newsgroup and
having some doubts of the kind it is a good idea to look for the group
Charter. A Usenet Big 8 Charter is not changeable for the life span of
the group and it has the absolute priority over any other group items
including FAQ, current consensus, authoritative opinion of a frequent
poster etc. Your case is very simple, as the clj Charter states and I
quote:
"... open to discussion on all aspects of JavaScript, as it relates to
HTML, Java, Perl, the World Wide Web in general, and other related
languages ..."
( http://www.faqs.org/ftp/usenet/news....ang.javascript
http://www.jibbering.com/faq/faq_not...s_charter.html )
So yes, your question is fully suitable and welcome for this
newsgroup. I will answer it later as it's a pm mad house here right
now :-)

Thomas 'PointedEars' Lahn 10-07-2009 06:30 PM

Re: Quick question about using a table of text input fields within a javascrip function
 
Ted Byers wrote:

> On Oct 7, 11:36 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>> The perl script is of no interest in this. It is either a javascript
>> question or it is not. If it is, please dump the perl code and just
>> post javascript and its associated javascript question.

>
> Your response, and Thomas' response, implies that there is no place to
> ask about mixed language programming.


No, the point the both of were trying to make is that it does not matter how
you generate ECMAScript-conforming (or -extending) script code if you have a
problem with that script code. And it is not logical to expect from people
interested in ECMAScript implementations to imagine what the output of a
Perl script would look like, even though there may be subscribers who know
both languages. So you should post the generated code instead.

And please post properly next time: <http://jibbering.com/faq/#posting> pp.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

Ted Byers 10-07-2009 09:32 PM

Re: Quick question about using a table of text input fields within ajavascrip function
 
On Oct 7, 2:30*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Ted Byers wrote:
> > On Oct 7, 11:36 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> >> The perl script is of no interest in this. It is either a javascript
> >> question or it is not. If it is, please dump the perl code and just
> >> post javascript and its associated javascript question.

>
> > Your response, and Thomas' response, implies that there is no place to
> > ask about mixed language programming.

>
> No, the point the both of were trying to make is that it does not matter how
> you generate ECMAScript-conforming (or -extending) script code if you have a
> problem with that script code. *And it is not logical to expect from people
> interested in ECMAScript implementations to imagine what the output of a
> Perl script would look like, even though there may be subscribers who know
> both languages. *So you should post the generated code instead.
>
> And please post properly next time: <http://jibbering.com/faq/#posting> pp.
>


Then both of you were wrong. Both of your responses imply reference
to a language other than javascript is inappropriate, and the charter
for this newsgroup clearly says this is wrong. According to both VK
and more importantly the charter of comp.lang.javascript (refer to
VK's reply to me here), my question/post is both appropriate and
correct.

While not everyone interested in ECMAScript will know perl, everyone
who knows perl will know exactly what that function produces, and it
is reasonable to expect there will be significant numbers of people
here who know both well enough to assist me. There is so little
"perl" in the snippet I provided, I doubt anyone had much difficulty
seeing what I was up to, and so had enough information to produce an
answer. Yes, it assumes a little more knowledge than someone who only
programs in javascript may have, but anyone who knows both javascript
and cgi programming in perl would have had no problem providing the
advice I'd requested. This is permitted by the charter for
comp.lang.javascript, which I understand is the ultimate authority on
such things.

My question was not about a problem with the javascript function
itself (that function works fine as is, if I were willing to ignore on
the server side what the user puts in the table on the client side),
but rather how to make it play nice with the perl script on the server
side.

I did read the FAQ and, once VK pointed me to it, the charter for this
newsgroup, BTW. Neither prohibit asking about interactions between
javascripts on the client side and perl scripts on the server side.
Since I was not asking for help debugging my code, I didn't think it
necessary to make it a standalone script within an HTML page (in fact
to do so would have required far more irrelevant material than the
code I posted).

Cheers,

Ted

Thomas 'PointedEars' Lahn 10-08-2009 01:03 PM

Re: Quick question about using a table of text input fields within a javascrip function
 
Ted Byers wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Ted Byers wrote:
>> > On Oct 7, 11:36 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>> >> The perl script is of no interest in this. It is either a javascript
>> >> question or it is not. If it is, please dump the perl code and just
>> >> post javascript and its associated javascript question.

>>
>> > Your response, and Thomas' response, implies that there is no place to
>> > ask about mixed language programming.

>>
>> No, the point the both of were trying to make is that it does not matter
>> how you generate ECMAScript-conforming (or -extending) script code if you
>> have a problem with that script code. And it is not logical to expect
>> from people interested in ECMAScript implementations to imagine what the
>> output of a Perl script would look like, even though there may be
>> subscribers who know both languages. So you should post the generated
>> code instead.
>>
>> And please post properly next time: <http://jibbering.com/faq/#posting>
>> pp.

>
> Then both of you were wrong.


*You* are wrong. And it is *you* who asking for assistance here, so when
told you were wrong, you should either comply and do what you are asked, or
go away. Because the people telling you are rather proficient in their
field and thus most certainly right (even if some of them are utterly wrong
at times), and any amount of arguing to the contrary is not going to get you
the answers that you seek. (It will only get you added to scorefiles and
killfiles.)

> [...]
> While not everyone interested in ECMAScript will know perl, everyone
> who knows perl will know exactly what that function produces, and it
> is reasonable to expect there will be significant numbers of people
> here who know both well enough to assist me.


No, it is not. It would be reasonable that a significant number of
subscribers of comp.infosystems.www.authoring.misc, for example, know
both well enough to assist you.

> My question was not about a problem with the javascript function
> itself (that function works fine as is, if I were willing to ignore on
> the server side what the user puts in the table on the client side),
> but rather how to make it play nice with the perl script on the server
> side.


So post the client-side ECMAScript script code along with the relevant
question, as you have been told. Because as much as it does not matter for
the Perl script that you are generating a client-side ECMAScript script with
it, it does not matter for the client-side ECMAScript script that you are
using Perl to generate it server-side (it would work the same with server-
side PHP, for example.)

If you are incapable of doing that, please go away (preferably to an
appropriate .misc group).

> I did read the FAQ [...]


But you did not adhere to its recommendations.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

VK 10-08-2009 02:14 PM

Re: Quick question about using a table of text input fields within ajavascrip function
 
> First question: how can I create, within this function, a hidden field
> that it to hold the number of new items added, in the first time it is
> invoked, and update it subsequently.


One way is to use closures over an inner function, but using closures
for such tasks IMHO is like invoking Satan to bring you doughnut from
the store :-) If you don't want to use global vars then simply use
function augmentation to store any needed static (in VB sense) data.

> Second question, I wrote this on the assumption that text input fields
> have to have unique names. Is this assumption correct, or can I give
> all the text input fields in this table the same name, so that in my
> perl script I can get all of them as an array (the perl CGI package
> allows one to get an array of all values for, e.g. options in a list
> box, as an array).


No, you don't have to NAME input differently, this applies to ID only.
At the same time to make your code more portable it maybe worth to use
different names in "BaseName[index]" format and to form array server-
side on submit. This way you JavaScript will be PHP friendly as well.
Just a suggestion.

Also I would highly suggest to use table-specific DOM methods rather
than generic ones.

The rest of changes are important/non-important depending on what
legacy environments you want to support. Say
inputElement.type = "someType"
will lead to security exception on IE6, one needs to use IE(5,6)-
specific twisted-around way to assign input types. Because
createElement('INPUT') creates by default type="text" and because this
is the type you need, we can make the code IE6-compatible by simply
removing this unnecessary assignment.

----------------------------------------

#!/usr/bin/perl

$tableID = 'Interface';

print <<EOT;
<DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Test</title>
EOT

&print_javascripts('item');

print <<EOT;
</head>
<body>
<form action="" method="GET">
<table id="$tableID">
<thead>
<tr>
<th>Test</th>
</tr>
</thead>
<tfoot>
<tr>
<td><input type="button" value="Add row"
onclick="addRow(\'$tableID\')"></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</tfoot>
<tbody></tbody>
</table>
</form>
EOT
exit(0);

sub print_javascripts {
print <<EOT;
<script type="text/javascript">
function addRow(id) {
var tbody = document.
getElementById(id).tBodies[0];

var input = document.createElement('INPUT');
var name = \'$_[0]\';
# PHP-friendly variant:
# var name = \'$_[0]\' +
# '[' + arguments.callee.n + ']';
input.name = name;
input.size = 50;
input.maxlength = 255;

((tbody.
insertRow(-1)).
insertCell(-1)).
appendChild(input);

arguments.callee.n++;
}
addRow.n = 0;
</script>
EOT
}

Thomas 'PointedEars' Lahn 10-08-2009 02:39 PM

Re: Quick question about using a table of text input fields within a javascrip function
 
VK wrote:

An attribution line is missing here.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
> [...]
>> Second question, I wrote this on the assumption that text input fields
>> have to have unique names. Is this assumption correct, or can I give
>> all the text input fields in this table the same name, so that in my
>> perl script I can get all of them as an array (the perl CGI package
>> allows one to get an array of all values for, e.g. options in a list
>> box, as an array).

>
> No, you don't have to NAME input differently, this applies to ID only.
> At the same time to make your code more portable it maybe worth to use
> different names in "BaseName[index]" format and to form array server-
> side on submit. This way you JavaScript will be PHP friendly as well.
> Just a suggestion.
>
> Also I would highly suggest to use table-specific DOM methods rather
> than generic ones.
>
> The rest of changes are important/non-important depending on what
> legacy environments you want to support. Say
> inputElement.type = "someType"
> will lead to security exception on IE6,


It does only lead to such an exception there if a `type' attribute or
property has already been specified.

> one needs to use IE(5,6)- specific twisted-around way to assign input
> types.


No, one does not.

> Because createElement('INPUT') creates by default type="text"


The reason behind this, is of course, that the default value of the `type'
attribute of INPUT/input elements is "text".

> and because this is the type you need, we can make the code IE6-compatible
> by simply removing this unnecessary assignment.


True.

> ----------------------------------------
>
> #!/usr/bin/perl
>
> $tableID = 'Interface';
> [...]


Yuck.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)


All times are GMT. The time now is 09:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.