Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Merging Perl/JS

by Grygonos (Chaplain)
on Sep 23, 2002 at 19:08 UTC ( [id://200202]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Brothers and Sisters of the perl...

I have a problem. I want to change the value of a form's submit button based on the value of a database query who's results are dependant on the values of two dropdown boxes in that same form.

so in the onchange() portion of the HTML dropdown I need the script to call a perl subroutine that makes the required query and then from that perl sub I would call the Javascript function with the result of the query as a parameter.

How can said mastery be accomplished?

Replies are listed 'Best First'.
Re: Merging Perl/JS
by belg4mit (Prior) on Sep 23, 2002 at 19:26 UTC
    It cannot.

    There are common ways of achieving the desired results. One is to submit the form onChange of the dropdown, and generate a new page with the appropriate information. The other is to store all of the possibly required information in the page and have the onChange act upon it accordingly. The latter is when I use Data::JavaScript::LiteObject although Data::JavaScript also works.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Merging Perl/JS
by dreadpiratepeter (Priest) on Sep 23, 2002 at 20:20 UTC
    You can do it, although you need to go through hoops and you may have browser issues. You need to set up a hidden communication machanism. You can do this by:
    1. Making an iframe with style='display: none;'
    2. Making a button with style='display: none;'
    3. Making a frame with style='display: none;'

    You can then use the src or location property of your hidden element to call a cgi script. They cgi script can use the onLoad handler to update the submit button.
    In addition you should make the onChange disable the submit button and the onLoad handler reenable it (so that they can't submit while you are calculating).

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
Re: Merging Perl/JS
by rbc (Curate) on Sep 23, 2002 at 19:24 UTC
    Please excuse me if this solution does not make any sense
    as I do not do a lot of web development and my Javascript
    is a bit rusty. But it would seem that you need to have your
    onChange() javascript function do a something like this ...
    function myOnChange(s) { var o = s.options[s.selectedIndex]; var t = o.text; document.location = "whatever?setButtonTo="+t; }
    where whatever?setButtonTo is a cgi that will re-write
    your form with the appropirate value for its submit button.

    Hope that helps :)
Re: Merging Perl/JS
by fruiture (Curate) on Sep 23, 2002 at 19:24 UTC

    That, sorry, sounds stupid to me: The value submit button of a form depends on the results of the form itself?

    You should rather look for a server-side-only method of organazing your information-flow.

    You cannot "combine" Perl (on the Server-side) and JS (on the Client-side) as you want it. Both don't know of each other's existence at all and they can only communicate implicitly. JS can cause requests that invoke the server-sided program and that program can generate JS code, which again is executed on "the other side of the web".

    I'm convinced there is a much better way of solving you particular problem using only Perl.

    --
    http://fruiture.de
Re: Merging Perl/JS
by andreychek (Parson) on Sep 24, 2002 at 01:02 UTC
    Actually, this is exactly what the OpenThought Application Environment was designed to do. Using an event like JavaScript's onChange(), you can send parameters to the server. The server can then respond in some way, and update portions of the existing document, all without reloading the page.

    There is a demo on the OpenThought website which will show you some of it's capabilities. One thing the online demo doesn't reflect at the moment is that OpenThought isn't limited to updating form fields, it can also dynamicaly update portions of HTML.

    The code for that demo comes with the OpenThought distribution, so you can use that to help you get started.

    Good luck!
    -Eric

    --
    Lucy: "What happens if you practice the piano for 20 years and then end up not being rich and famous?"
    Schroeder: "The joy is in the playing."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://200202]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found