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

Please help in understanding a piece of perl/js code

Status
Not open for further replies.

lcs01

Programmer
Joined
Aug 2, 2006
Messages
182
Location
US
Hi, Experts,

I am maintaining some of company's perl/javascript codes and having problems in understanding them. One piece of code is listed below:

Code:
my $fieldsList = join(',',@fieldList);

$jsSetupCode = <<EO_SETUP_CODE;

var listOfFields = new Array($fieldsList);

function saveData() {
  document.aForm.fields.value = listOfFields;
  document.aForm.submit();
  return(false);
}

EO_SETUP_CODE

print 
  header(),
  start_html(-title => 'My Title',-script => $jsSetupCode), 
  start_form(-name=>'aForm',-action=>'save.pl'),
  hidden(-name=>'fields',-value=>''),
  submit(-value=>'Submit', -onClick=>'saveData(); return(false)'),
  end_form(),
  end_html();

By design, upon clicking "Submit" button, the hidden CGI variable is assigned with a value of a js variable 'listOfFields' and then passed into save.pl. In real world, 99% of time the code is executed as expected. However, once a while, the CGI variable 'fields' is blank and thus save.pl does not know what to do. The worse part is that we can never reproduce it in house. But we do see it happened multiple times on our live server when our clients are using it remotely.

When this error happens, I have made sure that '@fieldList' is NOT null which is verified by the logs. I also studied the source codes very very carefully to make sure that 'listOfFields' was not overwritten before saveData() is called. But I can NOT prove that through logs, simply because it is executed on the clients' sides.

I suspect this is due to somehow 'saveData()' did not submit document.aForm.fields.value as designed, so that the CGI variable 'fields' becomes blank. But why? How can this happen?

BTW, one might suggest not using 'saveData()' and directly passing $fieldsList into save.pl:

Code:
hidden(-name=>'fields',-value=>"$fieldsList"),

Well, the answer is that 'saveData()' actually does more than I listed here. For example, it also validates 'listOfFields', which is omitted in this thread.

I hope I have made myself clear and look forward to your help. Thanks.
 
I am sure you could do the validation stuff that you omitted using perl instead of javascript. That way you could at least eliminate the javascript as part of the problem if the problem still persists.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Top of my head things:

Unless the source code delivered to the browser is incorrect (i.e., @fieldList is empty), then this is not a perl issue, but a browser/js issue. One way of checking is to append the value of @fieldList to a timestamped log file (with IP addresses) that you can cross reference with the HTTP logs. No blank lines means your perl is good.

Cross-reference the occurences of this error with the HTTP logs and check for common issues (particular user-agents or settings).

re-check saveData() to ensure that the validation process is not somehow causing listOfFields to be corrupted.

Sorta in common with the user-agents issue, it may be that some browsers are interpreting 'fields' as a collection, rather than as a particular field. Try renaming the field to be a little more descriptive.

At this point, you don't know where the 'blank' is occurring: Perl, Javascript, or as a browser artifact. Identifying that portion is where I'd put my energies first.
 
Thank you, brigmar. Those are indeed very useful suggestions. I'll certainly try that.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top