Solved

Perl, and a little javascript too.

Posted on 1998-12-31
5
128 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now