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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending data to an existing form

Status
Not open for further replies.

chance42

Technical User
Aug 5, 2004
22
US

Hello all,

I am having a problem while attempting to send a hidden
input field to an existing for elsewhere in my cgi script.

The PERL part works fine, but when I put the following
javascript line in, it breaks
Code:
Where tasks is the name & id of the form.
and fcheck is the name & id of the field.

document.forms.tasks.fcheck.value = "done";

I have also tried:

document.form.tasks.fcheck.value = "done";

as well as:

document.tasks.fcheck.value = "done";

I am at a loss. I am not completely briliant with Javascript
so perhaps I'm just missing a vital piece somewhere.
I've searched a bit but I cannot really seem to find
much on this, just a lot of reading form data and sending data to e-mail stuff, but from what I found it seems that the last one should work.

Help.

Thanks,

Mike


 
if fcheck is an id
Code:
document.getElementById("fcheck").value="done";
if tasks & fcheck are name
Code:
document.forms["tasks"].elements["fcheck"].value="done";
Hope it helps.
 
Thanks 13sio,

Unfortunately neither of those worked.

The JS debugger in Mozilla does not show any errors
but IE complains with the following errors:
Code:
When using this document.getElementById("fcheck").value="done";
IE gives this error:
Error: 'document.getElementById(...)' is null or not an object


When using this document.forms["tasks"].elements["fcheck"].value="done";
IE gives this error:
Error: 'document.forms.tasks.elements' is null or not an object

It was the same for the lines I was using before
IE complaines Mozilla JS debugger says nothing
and the script stops working at that line.

Mike
 
Just FYI,

Here is the my Javascript in its entirty
Code:
<script language="JavaScript" type="text/javascript">
   function isform() {
    var shift = "$SHIFT";
    var count = document.forms.length;
alert("Count is at "+count);
      if ((count == 0) && (shift == "night")) {
alert("PERL worked.");           
         document.tasks.fcheck.value = "done";
alert("Javascript worked too");
      }
   }
</script>

The alerts are just for testing obviously.
shift gets its value from a PERL variable set elsewhere.
So far it hasn't made it to the last alert,
it dies on that previous line.

Mike
 
Let's try the following codes, they works fine for me
Code:
<script>
function go() {
  document.forms["task"].elements["fcheck"].value="done";
}
</script>

<form name="task">
<input type="text" name="fcheck">
<input type="button" value="go" onClick="go();">
</form>
and
Code:
<script>
function go() {
  document.getElementById("fcheck").value="done";
}
</script>

<form>
<input type="text" id="fcheck">
<input type="button" value="go" onClick="go();">
</form>
 
Let's try the following codes, they work fine for me
Code:
<script>
function go() {
  document.forms["task"].elements["fcheck"].value="done";
}
</script>

<form name="task">
<input type="text" name="fcheck">
<input type="button" value="go" onClick="go();">
</form>
and
Code:
<script>
function go() {
  document.getElementById("fcheck").value="done";
}
</script>

<form>
<input type="text" id="fcheck">
<input type="button" value="go" onClick="go();">
</form>
 

One problem with those it that it needs to be a hidden input.
Not that it really matters anyway because I still get
the same errors listed above.
 
can you please show some html? we can't always guess what you have coded.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Sorry for the delay...

I do (with this curent code) get an error on Mozilla
which reads "document.getElementById("fcheck") has no properties"

This Javascript is embeded in a PERL script
The PERL works perfectly. Here is the code:
(The HTML generated by it all follows)
Code:
#!/usr/bin/perl -W
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $q = new CGI;

# Check user is logged in and get user name and shift
if(defined $q->cookie('TASKLIST_LOGIN')) {
my $cookie = $q->cookie('TASKLIST_LOGIN');
(my $username, my $SHIFT) = split(/:/, $cookie);

# Set up date and time functionality
my @months = ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) ;
(my $sec, my $min, my $hour, my $day, my $month, my $year) = localtime(time);
$year += 1900;
if ($day < 10) { $day = "0$day"; }
if ($month < 10) { $month = "0$month"; }
my $MONTH = $months[$month];
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }

# initialize variables & Assign Values
my $date = ("$year$MONTH$day");
my $time = ("$hour:$min");
my $tasktime = param ('tasktime');
my $task = param ('task');
my $platform = param ('platform');
my $action = param ('action');
my $username2 = param ('name');
my $count = param ('count');
my $fcheck = param ('fcheck');
my $server_IP = "$ENV{'SERVER_ADDR'}";
my $base = "/var/[URL unfurl="true"]www/htdocs/midr/tasklist";[/URL]
my $logdir = "/var/[URL unfurl="true"]www/htdocs/midr/tasklist/tmplogs";[/URL]
my $dayt = ("$months[$month] $day, $year");
my $SHIFT2 = "$SHIFT";
$SHIFT2 =~ tr/n|d/N|D/;

## Main program begins ##

sub format {
# Begin Format function
 $tasktime = shift; $task = shift; $platform = shift; $action = shift; $username = shift; my $count = shift;
 if (-e "$logdir/$count$SHIFT") { exit; }
print ("<tr>\n");
print ('  <form method="post" id="tasks" name="tasks" action="' . "[URL unfurl="true"]http://$server_IP/cgi-bin/tasklist/$SHIFT"[/URL] . 's.cgi#x">' . "\n");
print <<FOO;
    <td>$tasktime</td>
    <td>$task</td><a name=x></a>
    <td>$platform</td>
    <td>$action</td>
    <td><input type="text" name="name" size="10" value=$username></td>
    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="$count">
    <input type="hidden" name="tasktime" value="$tasktime">
    <input type="hidden" name="action" value="$action">
    <input type="hidden" name="platform" value="$platform">
    <input type="hidden" name="task" value="$task">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
FOO
}  # End of format function

if ($count >= "0001")
# these three sections creates the shift log file (nightlog.html for the night shifts)
# and the individual log files for each task as it is completed.
  {
if ($SHIFT eq 'night') {
if (! -e "$base/nightlog.html") {
  open(OUT, ">$base/nightlog.html");
    printf OUT "<html>\n<head><title>$SHIFT2 Tasklist</title>\n";
	printf OUT "\n</head><body bgcolor=#9099b6><center>\n";
    printf OUT "<h2>Midrange Daily Tasklist<br />$dayt - $SHIFT2 Shift</h2>\n";
    printf OUT '<table width="95%" border=2 valign=center halign=center bgcolor=white>';
    printf OUT "\n<tr>\n";
    printf OUT " <td><b>Time-Date</b></td>\n";
    printf OUT " <td><b>Task</b></td>\n";
    printf OUT " <td><b>Platform</b></td>\n";
    printf OUT " <td><b>Action</b></td>\n";
    printf OUT " <td><b>Name</b></td>\n";
    printf OUT " <td><b>Time</b></td>\n";
    printf OUT "</tr>\n";
  close(OUT);
} }

   my $tempfile= "$logdir/$count" . "$SHIFT";
   open(OUT, ">$tempfile");
     printf OUT "<tr>\n";
     printf OUT " <td>$tasktime</td>\n";
     printf OUT " <td>$task</td>\n";
     printf OUT " <td>$platform</td>\n";
     printf OUT " <td>$action</td>\n";
     printf OUT " <td>$username2</td>\n";
     printf OUT " <td>$time</td>\n";
     printf OUT "</tr>\n";
   close(OUT);

# this part appends the task info to the shift log .html file
  if ($SHIFT eq 'day') { open(FILE, ">>$base/$date-am.html"); }
  if ($SHIFT eq 'night') { open(FILE, ">>$base/nightlog.html"); }
     printf FILE "<tr>\n";
     printf FILE " <td>$tasktime</td>\n";
     printf FILE " <td>$task</td>\n";
     printf FILE " <td>$platform</td>\n";
     printf FILE " <td>$action</td>\n";
     printf FILE " <td>$username2</td>\n";
     printf FILE " <td>$time</td>\n";
     printf FILE "</tr>\n";
   close(FILE);
}

if ($count == "190001")
# Creates date variable to use in renaming the log file
# to the correct date at the end of the night shift.
{
my $tempfile="$base/backday-temp";
open(TMP, ">$tempfile");
  printf TMP $date;
close(TMP);
}

if ($fcheck eq 'done') {
my $tempfile="$base/TEST-FILE";
open(TMP, ">$tempfile");
  printf TMP "Line of text for testing. check = $fcheck";
close(TMP);
}



# Main HTML code for this page
print ("Content-type: text/html\n\n<html>\n<head>");
print("\n<title>$SHIFT2 Tasklist</title>\n\n");
print ("\n\n" . '<meta http-equiv="refresh" content="35; url=' . $SHIFT . 's.cgi#x">' . "\n\n");
print <<FOO;
[COLOR=red]
<script language="JavaScript" type="text/javascript">
   function isform() {
    var shift = "$SHIFT";
    var count = document.forms.length;
alert("Count is at "+count);
      if ((count == 0) && (shift == "night")) {
alert("PERL worked.");
         document.getElementById("fcheck").value="done";
alert("Javascript worked too");
      }
   }
</script>
[/color]
FOO
print ("\n</head>\n" . '<body bgcolor="#9099b6" onload="return(isform());">' . "\n");
print ("<center>\n");
print ("<h2>Midrange Daily Tasklist - $SHIFT2 Shift</h2>\n");
print ("<table width=95% border=2 valign=center halign=center bgcolor=white>\n");
print ("<tr>\n<td width=5%><b>Time/Date</b></td>\n");
print (" <td width=5%><b>Task</b></td>\n");
print (" <td width=5%><b>Platform</b></td>\n");
print (" <td width=60%><b>Action</b></td>\n");
print (" <td width=5%><b>Name</b></td>\n");
print (" <td width=5%><b>Time</b></td>\n</tr>\n");


#### Tasks Begin Here ####
##########################

  if (-e "$logdir/190001night")
  { open(TMP, "$logdir/190001night"); while(<TMP>) {print $_; } close(TMP); }
  elsif (! -e "$logdir/190001night")
   { &format("19:00- Daily","Monitoring","AS/400",'<ul><li>Task number 1</li></ul>',$username,'190001'); }

  if (-e "$logdir/190002night")
  { open(TMP, "$logdir/190002night"); while(<TMP>) {print $_; } close(TMP); }
  elsif ((! -e "$logdir/190002night") and (! -e "$logdir/190001night"))
   { &format("19:00- Daily","Monitoring","NT",'<ul><li>Task number 2</li></ul>'," ","190002"); }
  elsif ((! -e "$logdir/190002night") and (-e "$logdir/190001night"))
   { &format("19:00- Daily","Monitoring","NT",'<ul><li>Task number 2</li></ul>',$username,"190002"); }

  if (-e "$logdir/190003night")
  { open(TMP, "$logdir/190003night"); while(<TMP>) {print $_; } close(TMP); }
  elsif ((! -e "$logdir/190003night") and (! -e "$logdir/190002night"))
   { &format("19:00- Daily","Tape Return Process","AS/400",'<ul><li>Task number 3</li></ul>'," ","190003"); }
  elsif ((! -e "$logdir/190003night") and (-e "$logdir/190002night"))
   { &format("19:00- Daily","Tape Return Process","AS/400",'<ul><li>Task number 3</li></ul>',$username,"190003"); }

  if (-e "$logdir/190004night")
   { open(TMP, "$logdir/190004night"); while(<TMP>) {print $_; } close(TMP); }
  elsif ((! -e "$logdir/190004night") and (! -e "$logdir/190003night"))
   { &format("19:00- Daily","Tape Management","AS/400",'<ul><li>Task number 4</li></ul>'," ","190004"); }
  elsif ((! -e "$logdir/190004night") and (-e "$logdir/190003night"))
   { &format("19:00- Daily","Tape Management","AS/400",'<ul><li>Task number 4</li></ul>',$username,"190004"); }

  if (-e "$logdir/190005night")
   { open(TMP, "$logdir/190005night"); while(<TMP>) {print $_; } close(TMP); }
  elsif ((! -e "$logdir/190005night") and (! -e "$logdir/190004night"))
   { &format("19:00- Daily","Backup","Unix",'<ul><li>Task number 5</li></ul>'," ","190005"); }
  elsif ((! -e "$logdir/190005night") and (-e "$logdir/190004night"))
  { &format("19:00- Daily","Backup","Unix",'<ul><li>Task number 5</li></ul>',$username,"190005"); }

  if (-e "$logdir/190006night")
   { open(TMP, "$logdir/190006night"); while(<TMP>) {print $_; } close(TMP); }
  elsif ((! -e "$logdir/190006night") and (! -e "$logdir/190005night"))
   { &format("19:00- Tuesday","Backup","FDMS RSC6000",'<ul><li>Task number 6</li></ul>'," ","190006"); }
  elsif ((! -e "$logdir/190006night") and (-e "$logdir/190005night"))
   { &format("19:00- Tuesday","Backup","FDMS RSC6000",'<ul><li>Task number 6</li></ul>',$username,"190006"); }

##### End of Tasks #####

# If user is not logged in then return them to the login page
} else {
    print $q->redirect(-location=>'../../midr/tasklist/login.html');
}

Here is the HTML generated:
Code:
<html>
<head>
<title>Night Tasklist</title>



<meta http-equiv="refresh" content="35; url=nights.cgi#x">

<script language="JavaScript" type="text/javascript">
   function isform() {
    var shift = "night";
    var count = document.forms.length;
alert("Count is at "+count);
      if ((count == 0) && (shift == "night")) {
alert("PERL worked.");
         document.getElementById("fcheck").value="done";
alert("Javascript worked too");
      }
   }
</script>

</head>
<body bgcolor="#9099b6" onload="return(isform());">
<center>
<h2>Midrange Daily Tasklist - Night Shift</h2>

<table width=95% border=2 valign=center halign=center bgcolor=white>
<tr>
<td width=5%><b>Time/Date</b></td>
 <td width=5%><b>Task</b></td>
 <td width=5%><b>Platform</b></td>
 <td width=60%><b>Action</b></td>
 <td width=5%><b>Name</b></td>
 <td width=5%><b>Time</b></td>

</tr>
<tr>
  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]
    <td>19:00- Daily</td>
    <td>Monitoring</td><a name=x></a>
    <td>AS/400</td>
    <td><ul><li>Task number 1</li></ul></td>
    <td><input type="text" name="name" size="10" value=TESTER></td>

    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="190001">
    <input type="hidden" name="tasktime" value="19:00- Daily">
    <input type="hidden" name="action" value="<ul><li>Task number 1</li></ul>">
    <input type="hidden" name="platform" value="AS/400">
    <input type="hidden" name="task" value="Monitoring">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
<tr>

  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]
    <td>19:00- Daily</td>
    <td>Monitoring</td><a name=x></a>
    <td>NT</td>
    <td><ul><li>Task number 2</li></ul></td>
    <td><input type="text" name="name" size="10" value= ></td>
    <td><input type="submit" value="submit"></td>

    <input type="hidden" name="count" value="190002">
    <input type="hidden" name="tasktime" value="19:00- Daily">
    <input type="hidden" name="action" value="<ul><li>Task number 2</li></ul>">
    <input type="hidden" name="platform" value="NT">
    <input type="hidden" name="task" value="Monitoring">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
<tr>
  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]

    <td>19:00- Daily</td>
    <td>Tape Return Process</td><a name=x></a>
    <td>AS/400</td>
    <td><ul><li>Task number 3</li></ul></td>
    <td><input type="text" name="name" size="10" value= ></td>
    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="190003">

    <input type="hidden" name="tasktime" value="19:00- Daily">
    <input type="hidden" name="action" value="<ul><li>Task number 3</li></ul>">
    <input type="hidden" name="platform" value="AS/400">
    <input type="hidden" name="task" value="Tape Return Process">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
<tr>
  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]
    <td>19:00- Daily</td>

    <td>Tape Management</td><a name=x></a>
    <td>AS/400</td>
    <td><ul><li>Task number 4</li></ul></td>
    <td><input type="text" name="name" size="10" value= ></td>
    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="190004">
    <input type="hidden" name="tasktime" value="19:00- Daily">

    <input type="hidden" name="action" value="<ul><li>Task number 4</li></ul>">
    <input type="hidden" name="platform" value="AS/400">
    <input type="hidden" name="task" value="Tape Management">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
<tr>
  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]
    <td>19:00- Daily</td>
    <td>Backup</td><a name=x></a>

    <td>Unix</td>
    <td><ul><li>Task number 5</li></ul></td>
    <td><input type="text" name="name" size="10" value= ></td>
    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="190005">
    <input type="hidden" name="tasktime" value="19:00- Daily">
    <input type="hidden" name="action" value="<ul><li>Task number 5</li></ul>">
    <input type="hidden" name="platform" value="Unix">

    <input type="hidden" name="task" value="Backup">
<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>
<tr>
  <form method="post" id="tasks" name="tasks" action="[URL unfurl="true"]http://67.166.1.61/cgi-bin/tasklist/nights.cgi#x">[/URL]
    <td>19:00- Tuesday</td>
    <td>Backup</td><a name=x></a>
    <td>FDMS RSC6000</td>

    <td><ul><li>Task number 6</li></ul></td>
    <td><input type="text" name="name" size="10" value= ></td>
    <td><input type="submit" value="submit"></td>
    <input type="hidden" name="count" value="190006">
    <input type="hidden" name="tasktime" value="19:00- Tuesday">
    <input type="hidden" name="action" value="<ul><li>Task number 6</li></ul>">
    <input type="hidden" name="platform" value="FDMS RSC6000">
    <input type="hidden" name="task" value="Backup">

<input type="hidden" name="fcheck" id="fcheck" value="nogo">
  </form>
</tr>

</body>
</html>

Thanks,
Mike
 
instead of using getElementById, what happens when you do this:

Code:
document.forms['tasks'].elements['fcheck'].value = 'done';

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 

Thanks everyone for their help,

I figured it out.

The problem was that I was not passing the data into the function where the form was...
If I place the <input....> line outside of the function
then it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top