http://qs321.pair.com?node_id=179740

peacemaker1820 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

I am trying to connect to my database while the user is filling out the form. It works as such: the user fills out a text box, without submitting the form, I would like to have a pop up window from where the user can have more options that are coming from a database.
Here is my code:
IN HTML I HAVE:
<input type="text" name="textBox" size="8"> <a href="javascript:priceList('form.textBox.value');">Price List</a>
IN JAVASCRIPT I HAVE:
function priceList(text){ win=window.open("cgibin/getPrice.pl?textBox="+text,"","width=250,heigh +t=240") win.creator=self }
IN PERL I HAVE:
use CGI; $q = new CGI; $partNumber = $q->param('textBox'); print ($partNumber);

Thus, as you can see in Perl I am printin the result of what i have got. The result on page that I get is UNDEFINED.
What am I doing wrong?
Thanks Monks I am out of guesses!

Replies are listed 'Best First'.
Re: Retrieving value from HTML->Javascript->Perl
by Speedy (Monk) on Jul 05, 2002 at 23:16 UTC

    Perhaps the problem is in getting the value of "textBox" passed in the JavaScript function call.

    Try reading the value in the JavaScript function rather than passing it. This also would give you the chance to test for no value or improperly formatted values being entered.

    For example, in your HTML code, try something like:

    <html> <head> <script language="JavaScript"> function priceList () { var text=form.textBox.value; win=window.open("/cgi-bin/getPrice.pl?textBox="+text,"","width=250,hei +ght=240") } </script> </head> <body> <form name='form'> <input type="text" name="textBox" size="8"> <a href="javascript:priceList();">Price List</a> </form> </body> </html>

    with about the same Perl script, for example:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = new CGI; my $partNumber = $q->param('textBox'); print $q->header, $q->start_html; print $q->p("Part Number = $partNumber"); print $q->end_html;

    The pop-up window then contains the value entered.

    Speedy

    Live in the moment

Re: Retrieving value from HTML->Javascript->Perl
by dws (Chancellor) on Jul 05, 2002 at 22:06 UTC
    The result on page that I get is UNDEFINED.

    I'm not clear on whether you're getting a 404 page in the popup, or whether the CGI is producing empty results.

    If the former, I suspect you have a problem with this line   win=window.open("cgibin/getPrice.pl?textBox="+text, Typically, the CGI directory is named "cgi-bin", not "cgibin". Double-check this. Also, you might have better luck using a fully qualified URL. E.g.,   win=window.open("http://www.example.com/cgi-bin/getPrice? ...

Re: Retrieving value from HTML->Javascript->Perl
by George_Sherston (Vicar) on Jul 05, 2002 at 21:35 UTC
    Apart from your not using strict (which I and I should think everyone else round here would encourage you to do), I don't think there's a problem with your Perl. If you're outputting to a web browser you'll need
    print $q->header, $partNumber;
    but you probably knew this.

    I think the problem is likely to be in your jscript, and two possibles are
    • I think form.textBox.value should perhaps not be in quotes
    • Do you need cgi-bin instead of cgibin?


    § George Sherston
Re: Retrieving value from HTML->Javascript->Perl
by TexasTess (Beadle) on Jul 07, 2002 at 00:04 UTC
    The following screams at me from your code:
    win=window.open("cgibin/getPrice.pl?textBox="+text,"","width=250,heigh+t=240")

    Should read:
    win=window.open("cgibin/getPrice.pl?textBox='+text+' ","width=250,height=240")

    See the difference?

    TexasTess
      Should read:
      win=window.open("cgibin/getPrice.pl?textBox='+text+' ","width=250,height=240")

      Nope. Not unless the CGI expects the string to arrive with single quotes around it. I suspect that the right thing to do here is to URL-encode 'textBox', via

      win=window.open("cgibin/getPrice.pl?textBox=" + escape(text), "width=250,height=240")
        I've just written some code almost exactly like this..but it's at work and I'm at home..and my brain is fried so can't remember the EXACT syntax I used however....

        I've found that when trying to use quotes inside a quoted string the CGI converts the ' nicely...but leave the " in and it screws it up...ALSO shouldn't there be a "+" on either side of the variable?
        Texas Tess