Perl, and a little javascript too.

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.
robinzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
martinagCommented:
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
 
tw525Commented:
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
 
yorenCommented:
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
 
sdjjmCommented:
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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
robinzAuthor Commented:
I have large testicles
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.