从Javascript调用perl文件/函数和函数来更新HTML

I have a form with two inputs. The first one is a regular text input. The second one is a text inpu side by side with a dropdown menu list.

Once the first input is entered, the user should enter a number into the second input. The second input is essentially a sublist of numbers available under the first selected input number (think of it as a multidimensional array).

Once the onFocus or onChange is set to the second input I want to execute a the onfocus/onChange JavaScript functions and make them run a perl script. The results of the perl script should populate the dropdown list - so the user essentially will be able to select or type in the number they wish to choose.

I found this in order to make a call to a PHP function - but my function accepts only one variable and after I modified it I get or the default result or `No function arguments'

What would be the right way to execute a perl script from a JS function and then get the perl results and populate in the HTML? (I know it is broad, but a direction would be nice, and example would be great)

Before answering I'll make some assumptions explicit:

  1. Javascript will run in a browser client-side, Perl on the server side
  2. the whole server side is programmed in Perl, so you already selected one of many frameworks that allow you to build a dynamic site in Perl

This said, in the Javascript code on the client-side you basically don't care which programming language is answering on the server side. You have to decide where your server will answer - e.g. on "/list2-alternatives/" - and assuming that you only want to pass one single "simple" parameter, you can encode it directly in the URI (e.g. "/list2-alternatives/XXX" and "/list2-alternatives/YYY" depending on what comes from the first field). This goes for the client side.

On the server side, you will have to ensure that the particular path returns the data you're after.

This is what will happen:

  • user sets a value for field1 on the client, let's say it is the string "whatever"
  • when onChange/onFocus are triggered, the client sends a GET request towards http://your-server/list2-alternatives/whatever to the server
  • your Perl code is listening on the server side (in "your-server") and receives a GET for path "/list2-alternatives/whatever", produces a response and sends it back to the client
  • the Javascript code on the client side receives the response and uses the data to change the second field.