Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript in perl cgi page 1

Status
Not open for further replies.

chance42

Technical User
Aug 5, 2004
22
US
Hello all,
It’s time once again to admit that I cannot do
everything myself and ask for help.

I have created a web based to-do list and have a bit of javascript that will tell when the last Task has been
completed no matter the order of completion.

The problem I am having is that upon the completion of the
last task I need to rename a file that is logging everything
but I cannot figure out how to have the javascript relay
that information to the PERL script.

I would like to just assign a variable based on the
exit status of the javascript if possible, So that I could
just have a line like:
Code:
if ($check = true) { do my bidding; }

Here is the javascript:
(The alert is just for testing and will not be used
during normal operaation.)

Code:
<script language="JavaScript" type="text/javascript">
function isform() {
element = document.getElementById("tasks");
  if (element == null) {
    alert("All tasks are complete.");
  }
}
</script>

right now I have it set up so when the last task listed is
submitted then the log file is renamed, but I found that the
users will do things out of order, causing tasked not to be logged.. I know it must be possable to do it this way, however I getting ready just to add an extra task that has the user confirm that the rest of the list is done.

I fairly sure it's simple and I'm just forgetting something.
But I've been at a loss for about three weeks.

Thanks for your time,

C
 
if you have a hidden field in the form you could set that, and have the perl script pick up the value.

HTH
--Paul

cigless ...
 
Paul's solution seems ideal to me


Kind Regards
Duncan
 
If there is a server side process creating an activity log, why can't it figure out when the task is done?

Assuming the server side process isn't up to the task, how about piggy backing Notification Of Completeness onto whatever mechanism keeps the server log up to date?

Or, use the onUnload event. Attach it to the Body tag --

Code:
<body onunload="isform();">

BTW, asking the user to confirm completion of all tasks is a desperate act. The only way to trust the user's response is to independently verify it. If you can independently verify, why ask?
 
Thanks everyone,

I had tried Pauls method several different ways but I couldn't get it to work correctly...

I have tried a variety of different ways repeatedly
for a few weeks sometimes getting different results
from the same code.

I have found a solution though, it's not as elegant
as I would like and I kind of feel like I am cheating
but I ened up adding a redirect to the javascript
to another small cgi file that then renames the log.
It's not purty but it works and my head has stopped
pounding so it's all good as far as I'm concerned.

Thanks again,

Mike
 
Hi Mike

To expand on Paul's idea of setting a hidden field value...

(You seem like you know what you are up to with the javascript - i'm not so I won't enter into that part)

Code:
<script language="JavaScript" type="text/javascript">
function isform() {
element = document.getElementById("tasks");
  if (element == null) {
    alert("All tasks are complete.");
    [red]document.form.hiddenfield.txt.value = "all_completed";[/red]
  }
}
</script>

then the GET or POST form submission will carry this hidden field value - along with all the visible ones - to the Perl script

use this code for a POSTed form - for example:-

Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split (/&/, $buffer);

foreach (@pairs) {
  ($key, $value) = split (/=/);
  print "$key <font color=red>=></font> $value<br>\n";
}


Kind Regards
Duncan
 

Thanks Duncan,

My page is a series of many forms that send both
regular and hidden data, my problem was that the JS
worked outside the form (at least as far as determining
when the last task was submitted) but not inside
where I needed the hidden input line to be written.
 
It's probably my poor understanding of Javascript - and I apologise if this is incorrect - but surely if you can read form values with your JS, then you can write form values with the JS - and then submit the page...

also, where the JS is located is surely not relevant?

i really do apologise if i am missing the point!


Kind Regards
Duncan
 
doesn't the DOM (document object model) allow you to have complete control - regardless of how many forms you have?


Kind Regards
Duncan
 
you are saying that the javascript is outside the form. That isn't a problem. Just add another hidden field to the form and have the javascript set it.

like:
document.yourformname.newhiddenfield.value = "newfilename";
--just like what was said.

then just parse it with your cgi as usual. If you are trying to have javascript change the variable of a perl script it cant be done with sending a request or some sort.
 
there are tek-tips cgi & javascript forums

javascript is event driven. common events include when the script is initially loaded, when a field changes, when the script is submitted, when a button is clicked.

javscript can be in the "wrong place", for example, if it is executed before the script is fully loaded, immediately after intial load, or before the client has made any change to a field of interest.

"onSubmit" handler(s) should catch "upon the completion
 
arn0ld, star 4 that,

Mike, ascii art I don't do every day

+document------------+
|+form1--------------+
+|field1-------------+
+|field2-------------+
+|field3-------------+
++field14------------+
+----------hidden1---+
|-form1--------------+
|+form2--------------+
+|field1-------------+
+|field2-------------+
+|field3-------------+
++field14------------+
+----------hidden1---+
+-form2--------------+
+====================+

document.form1.hidden1 is not the same as
document.form2.hidden1

dunno if it makes any sense
HTH
--Paul

cigless ...
 
Code:
+document------------+
|+form1--------------+
+|field1-------------+
+|field2-------------+
+|field3-------------+
++field14------------+
+----------hidden1---+
|-form1--------------+
|+form2--------------+
+|field1-------------+
+|field2-------------+
+|field3-------------+
++field14------------+
+----------hidden1---+
+-form2--------------+
+====================+
What it was meant to look like ....?


cigless ...
 

OK, you couldn't let me live with my crappy way
of doing it, and it doesn't take much for me to
start messing with things again...

first my crappy code:

Code:
<script language="JavaScript" type="text/javascript">
   function isform() {
    var element = document.getElementById("tasks");
      if (element == null) {
         window.location="fini.cgi";
      }
   }
</script>

fini.cgi just renames the log file via PERL.

Now the new code which breaks things:

Code:
<script language="JavaScript" type="text/javascript">
   function isform() {
    var fcheck = "nogo";
    var element = document.getElementById("tasks");
      if ((element == null) && ($SHIFT == "night")) {
         document.tasks.newhiddenfield.fcheck = "done";
      }
   }
</script>

And elsewhere in my file I have the following code
that is just test stuff now, but would contain the
code to rename the file - if things worked.
Code:
if ($fcheck == "done") {
my $tempfile="$base/TEST-FILE";
open(TMP, ">$tempfile");
  printf TMP "This is a test line of text";
close(TMP);
}

Unfortunately as soon as I open the web page
it reacts as if all forms are complete and writes the
test string to the test file.

Note: all I did was replace the redirect with the line
to add the hidden info to the form

Now, I have been up for about 33 hours so maybe
my internal logic is on the fritz but I do not see
what the problem is...
 

Whoops, the line

Code:
if ((element == null) && ($SHIFT == "night")) {

really is in both...

Mike
 

Ok I changed my code to:

Code:
<script language="JavaScript" type="text/javascript">
   function isform() {
    var shift = "$SHIFT";
    var element = document.getElementById("tasks");
      if ((element == null) && (shift == "night")) {
         document.forms.tasks.fcheck.value = "done";
      }
   }
</script>

But when the page first loads and everytime it reloads
It processes the instructions for what to do when
the instructions

 
element is never going to be null unless you don't have a
Code:
<tag id="tasks">
.... right? what is element returning?
my javascript is a bit rusty but that seems odd.
 
Code:
var shift = "$SHIFT";

Where is $SHIFT set, in a perl script?
--Paul

cigless ...
 
Sorry folks,

It's now a javascript problem.
The PERL part works perfectly, there is something wrong
with the line:
Code:
document.forms.tasks.fcheck.value = "done";

I have searched on it quite a bit and I have tried
a few different versions of it but it still doesn't work
so I guess I will have to switch to the JS forum

Thanks eveyone for your time and effort.

Mike
 
ah well - 42 missed his chance.

if using netscape/mozilla, a nifty javascript debugging console is available. probably one on IE( whatzat?)

document.forms is an array. Either refer to: document.forms[index_to_my_form].fcheck.value or
document.my_form.fcheck.value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top