in reply to how to make dependent drop down in perl script

Example :
 1st drop down : Country Name :
 2nd Drop down : State Name

so by selecting Country name from 1st drop down , the State name get populated .

Both value (Country name and State name) are populated from database .

  • Comment on Re: how to make dependent drop down in perl script

Replies are listed 'Best First'.
Re^2: how to make dependent drop down in perl script
by cavac (Prior) on Mar 13, 2023 at 13:07 UTC

    Since you want to do this with HTML, this will require some Javascript as well. I rather suspect that somthing like

    <select id="country" onchange="changestatelist()"> .... <script> function changestatelist() { ...

    will be involved.

    w3schools has a tutorial.

    What it boils down to: If you make a website, you'll need to learn HTML, Javescript and CSS in addition to your backend language (perl, php, python or nodejs).

    Edit: As a sidenote, especially for my friends in th USA: Not all countries have states or regions, especially the smaller ones. Monaco and Liechtenstein are small enough for a single postal worker to know all the adresses. And is Sealand ever gets recognized, you can tour the whole country in about 15 minutes flat.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Thanks for responding and support. FYI : I have added OnChange method in first dropdown and defined the method in javascript which get called appropialy onchange. Now when the value get change in first dropdown , we get the selected value in javascript method , now I want to pass this value in perl method to retrive 2nd dropdwon value . but her I am unable to call the perl method from javascript/ Ajax . Please help me how to call perl method from javascript/Ajax ?

        Ajax just does a HTTP request to the URL you specify. On the server side, you handle it like any other web request.

        I don't have the energy to write a custom example, i'm still suffering from an inflamed right wrist.

        This example is just taken straight from my own framework (in this example, we are checking if the filename already exists on the server before an upload). First we need some JavaScript to define to do the Ajax call, see images.tt:

        function ajaxUpdateFName() { if(alreadyUpdating) { return; } alreadyUpdating = 1; $( "#upbutton" ).button( "option", "disabled", true ); var origname_c = document.getElementById("upfile"); var newname_c = document.getElementById("upfname"); var description_c = document.getElementById("updescription"); var params = "fname=" + origname_c.value; srefresh.open("POST", ttvars.checkfname, true); //Send the proper header information along with the request srefresh.setRequestHeader("Content-type", "application/x-www-for +m-urlencoded"); srefresh.onreadystatechange=function() { if(srefresh.readyState == 4) { var serverresponse = srefresh.responseText; var parsed = JSON.parse(serverresponse); newname_c.value = parsed.fname; $("#ajaxwait").hide(); $("#upfnamestatus").html(parsed.statustext); if(parsed.description != '' && description_c.value == '') { description_c.value = parsed.description; } if(parsed.status == 'OK') { $("#upfnamestatus").css("color", "green"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else if(parsed.status == "WARNING") { $("#upfnamestatus").css("color", "orange"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else { $("#upfnamestatus").css("color", "red"); } $("#upfnamestatus").show(); alreadyUpdating = 1; } } srefresh.send(params); }

        On the Perl side, we then handle the request and send a document to the client, see Images.pm:

        sub get_fname($self, $ua) { my $dbh = $self->{server}->{modules}->{$self->{db}}; my $filename = $ua->{postparams}->{'fname'} || ''; $filename = $self->clean_fname($filename); my %data = ( fname => $filename, status => 'OK', statustext => 'OK', description => '', ); # Check for existing filename my $selsth = $dbh->prepare_cached("SELECT * FROM " . $self->{tablename} . " WHERE filename = ? LIMIT 1") or croak($dbh->errstr); $selsth->execute($filename) or croak($dbh->errstr); my $existcount = 0; while((my $line = $selsth->fetchrow_hashref)) { $existcount++; $data{description} = $line->{description}; } if($existcount) { $data{status} = 'WARNING'; $data{statustext} = "File exists, will overwrite!<br/><b>This +may lead to caching problems for the next few hours!</b>"; } my $jsondata = encode_json \%data; return (status => 200, type => "text/plain", data => $jsondata); }

        The code in your project will probably look quite a bit different (different framework, different task), but the gist of it will be the same:

        1. JavaScript gathers data for the question you want to ask the server.
        2. JavaScript sends request to server and registers a callback function for when the answer arrives.
        3. Perl server gets HTTP request.
        4. Perl works on the request.
        5. Perl sends an answer in the form of a HTTP reply to the client.
        6. Javascript gets the reply and does something with the data provided.

        Hope that helps.

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP