Current location: Hot Scripts Forums » Programming Languages » Perl » Transfer values from JavaScript to cgi


Transfer values from JavaScript to cgi

Reply
  #1 (permalink)  
Old 07-26-06, 08:49 AM
agent186 agent186 is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Transfer values from JavaScript to cgi

Hi all,
This is my first post, so please be gentle. I have also posted this in the Perl forum because it uses both technologies. The end goal is to collect some client side values (browser type, OS, screen resolution, etc.) and then pass those values to a cgi script so they can be entered in a database. Another option is to write the data to a log file, or better yet use cgi to collect the variables directly (don't think that's possible). I have been fulling around with AJAX, but can't quite get it to work. Here's what I have for the JavaScript...
Code:
   var http_request = false;
   function makeRequest(url) {

       if (window.XMLHttpRequest) { // Mozilla, Safari,...
           http_request = new XMLHttpRequest();
       } else if (window.ActiveXObject) { // IE
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
       }
       http_request.open('GET', url, false);
       http_request.send(null);
   }
The function is executed in the onclick method ...
Code:
<a href="#" onclick="makeRequest('http://www.myurl.com/collect.cgi?browser=IE')">Make a request</a>
I hard coded the parameter for testing purposes only. Once I get it working right I can easily create the parameter based on the browser the user is using.

Here is the cgi script (again, short and sweet for testing only) ...
Code:
#!/usr/bin/perl
use CGI qw(:standard);
my @params = param();
open(WRITER, "> output.txt");
print WRITER "Params: " . scalar(@params);
close(WRITER);
All it does is write the number of parameters it recieves to a file on the server.

What is happening is that the parameters are not being passed, and the cgi sctript executes over and over and over, rewriting the file even after the browser is closed and the output.txt file is deleted. The only why I can stop it is to delete the cgi script itself!

I am perfectly happy with taking an entirly differant approach if anyone knows of one. Or if you can tell me why my scripts are not working, that would be great too.

Thanks in advance for any insight.
Reply With Quote
  #2 (permalink)  
Old 07-26-06, 10:45 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Please do not double post:
http://www.programmingtalk.com/showthread.php?t=31272
Two threads are not needed for a single problem. If the problem is discovered not to be related to the section the thread is in, a moderator can simply move it to keep the entire discussion in one place.

Anyway, I can't find anything wrong with the syntax of the JavaScript code. And unless you get an error on the client saying otherwise, the problem lies on the server.

EDIT: Didn't see Nico's post... Thread moved to the Perl section.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 07-26-06 at 10:49 AM.
Reply With Quote
  #3 (permalink)  
Old 07-26-06, 10:55 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by agent186
Hi all,
This is my first post, so please be gentle. I have also posted this in the JavaScript forum because it uses both technologies. The end goal is to collect some client side values (browser type, OS, screen resolution, etc.) and then pass those values to a cgi script so they can be entered in a database. Another option is to write the data to a log file, or better yet use cgi to collect the variables directly (don't think that's possible, but if you know of a way, please let me know). I have been fulling around with AJAX, but can't quite get it to work. Here's what I have for the JavaScript...


Here is the cgi script (again, short and sweet for testing only) ...
Code:
#!/usr/bin/perl
use CGI qw(:standard);
my @params = param();
open(WRITER, "> output.txt");
print WRITER "Params: " . scalar(@params);
close(WRITER);
All it does is write the number of parameters it recieves to a file on the server.

What is happening is that the parameters are not being passed, and the cgi sctript executes over and over and over, rewriting the file even after the browser is closed and the output.txt file is deleted. The only why I can stop it is to delete the cgi script itself!

I am perfectly happy with taking an entirly differant approach if anyone knows of one. Or if you can tell me why my scripts are not working, that would be great too.

Thanks in advance for any insight.
Ok, let's take it from the top. I think you are approaching this the wrong way. Why, because a person may not even have javascript enabled in their browser so you are writing "no data" to the log file.

1) What environmental variables are you trying to capture from each visitor?

2) You mentioned a "on-submit"... What are they submitting to? A CGI application?

3) Without being too harsh on your Perl code, I think you may want to learn the difference in the next two lines of code.

open(WRITER, "> output.txt"); # Create output.txt even if one is exists.
open(WRITER, ">> output.txt"); #Open output.txt for editing as ">>" tells the interpreter that the file should already be there.

4) Always begin any Perl script with use strict; use warnings; UNLESS you are *very* good at coding Perl.

Answer question #1 and then we can begin whipping something up for ya.

Last edited by curbview.com; 07-26-06 at 10:59 AM.
Reply With Quote
  #4 (permalink)  
Old 07-26-06, 11:40 AM
agent186 agent186 is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
1) What environmental variables are you trying to capture from each visitor?
I want to capture things like the Operating System, the Broswer type and version, the current URL, and the referrer (among other things, but those are the most important).

2) You mentioned a "on-submit"... What are they submitting to? A CGI application?
There's no "on-submit", just an onclick method for now. I only did this for testing. It simply executes the JavaScript function. How the function gets executed may change. The cgi script is called from the JavaScript function.

I do undertand the differance between ">" and ">>". I only have it that way for testing. In production it will write to a database, I just didn't want to code all that if I can't get the variables to pass.

The code that have is just a quick and dirty test so that I can make sure that passing values from JavaScript to cgi is possible. If there is a way that Perl can pick up client side enviornment variables, I think that would be easier, but I have not been able to find anything on how to do that.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
Adding values to text field from popup - JavaScript danielle_dee JavaScript 0 07-28-04 08:54 PM
Transfer value from html to cgi PlaneLady HTML/XHTML/XML 1 05-20-04 12:17 AM
MySQL table problem perleo PHP 9 12-16-03 01:16 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 04:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.