Solved

Perl, and a little javascript too.

Posted on 1998-12-31
5
131 Views
Last Modified: 2010-03-05
Can I use "Content-type: text/html" to output html to the browser, use a javascript prompt to get info from the user, and then reference that javascript variable from my Perl code?  This is messing with my head, please help.
0
Comment
Question by:robinz
5 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1207233
You can't use it in the same process of the script that is printing the page.

However, you can recall the script when you've got what you need:
<SCRIPT LANGUAGE="JavaScript">
<!--
s = prompt("Enter something.","");
if (s)
  location.href = location.href + ((location.href.indexOf('?')==-1) ? "?" : "&") + "variableName=" + escape(s);
// -->
</SCRIPT>

This will call the script again and pass the variable to it.

Martin
0
 
LVL 1

Expert Comment

by:tw525
ID: 1207234
I haven't tried using Javascript to gather the variable name but you might want to try using the "Content-type: text/html" command to write a page with a form submission, the call a second Perl program from  the newly created code.

For example:  

print" <FORM method=\"POST\" action=\"services.pl\" name="\forma\"><SELECT name=\"season\" size=1><OPTION>Choose
a Main Topic&nbsp;";

This will start a form in Html, which when the submit button is hit it will call the Perl program services.

0
 
LVL 7

Expert Comment

by:yoren
ID: 1207235
Like the others said, it's not possible to do what you asked, and an alternative would be to call a second Perl script. Here's why you can't do it:

HTTP works in single, separate transactions. The browser asks for the page, the server gives it one. End of transaction. The next time the user hits a link is whole new transaction; the browser asks for the page, etc. When the link is a CGI, it works like this: the browser requests the CGI link, the server runs the CGI program, the server sends the browser the CGI's output.

Once the browser gets the results from the CGI, the only way it can communicate back to it is to make a whole new request. So, since your Perl CGI runs on the server, and Javascript runs in the browser, it can't be done.

What are you trying to do? Could it be possible to request this information from the user before the CGI is called -- from the previous form?

Yuval




0
 
LVL 1

Accepted Solution

by:
sdjjm earned 400 total points
ID: 1207236
You can print an entire HTML document (including Javascript) to the browser and then input information which you can use in another CGI.  Heres an example of doing what you want with a form.  You can do the same with your JS routine:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>Whatever</title>";
print "</head>";
print "<FORM ACTION=\"cgi-bin/newscript.cgi?data\" METHOD=post>";
print "<input TYPE=\"text\" NAME=\"data\" SIZE=\"35\">";
print "<input TYPE=submit VALUE=\"Enter\"></FORM>";
etc...etc...etc

When you get your user input (from the FORM you printed) you're calling another CGI (newscript.cgi) and  the data will pass to  it via the "?data" parameter.  Your called CGI will have to read that data to process it.  You can use something like the code below and the data you passed will be in the string value "$user_string" for you to do what ever you want with:

#!/usr/local/bin/perl
read(STDIN,$user_string,$ENV{'CONTENT_LENGTH'});
if (length($ENV{'QUERY_STRING'})>0) {$user_string=$ENV{'QUERY_STRING'}};
$user_string =~ s/\+/ /g;    # here's the data you passed

0
 

Author Comment

by:robinz
ID: 1207237
I have large testicles
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question