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

Hi Monks,
I have 2 drop downs in a page one with Student name and second with label exams list which he has appeared. I am obtaining the values for these 2 fields from database table by below query
. @exams ="select EXAMS_APPEARED from student_details where student_name="xyz" order by 1"
I am using the values of these @exams as options in second drop down. When i select student name from first drop down , the second drop down should display only the exams which he has appeared . For this i have written a javascript function as below
#java script function function displayExams() { var student = document.getElementById("STUDENT_NAME").value; var exams=document.getElementById("EXAMS_APPEARED").value;
and calling this function with onclick event like as below
select style='$style_input' name='EXAMS_APPEARED' id='EXAMS_APPEARED' +onClick=\"displayExams();\" > <option value=''>--Select--</option> "; my $ExamsCount=@ExamsAppeared; for (my $j=0;$j<$ExamsCount;$j++) { $DisplayString=$DisplayString."<option value='$ExamsAppeared[$j]'>$Exa +msAppeared[$j]</option>\n"; } @ExamsAppeared = getdbresult(); sub getdbresult { # for getting the db data stmts... }
After opening the application webpage, when i select the student name from drop down, the second drop down on click should only display the exams which he has appeared, but it displaying exams_ appeared for all the student. Could you please rectify my mistake and help me to fix this issue.

Replies are listed 'Best First'.
Re: Unable to populate the aasociated values as options in a dropdown
by GrandFather (Saint) on Mar 24, 2015 at 03:59 UTC

    You seem to be confusing server side code (your Perl script) with client side code (JavaScript) and what can be done in each context. Maybe you've just been unclear about which code is where?

    I think what you are trying to achieve is to have a client side user facing list update as the user makes selections in another list. If that is the case then there are two quite distinct parts of the solution. The first part is to create on the server the master list (student id in this case) and a look up table indexed by master list entries. Use these two structures to populate the user facing student list and a hidden JavaScript look up table. I tend to wrap the table in a JS function that is generated for each page and use the function to return an appropriate list (exams in this case) in the OnClick handler call. Something like:

    onClick="displayExams(examList(document.getElementById('STUDENT_NAME') +.value));"
    Perl is the programming world's equivalent of English
Re: Unable to populate the aasociated values as options in a dropdown
by roboticus (Chancellor) on Mar 24, 2015 at 03:49 UTC

    It sounds like you should populate the exams field *after* the user selects the student name. To do so, you could use AJAX to send the student choice to server and have it query the exams and send them back. Alternatively, when you create the page, you could send a data table containing a cross reference of students vs exams, and use the event generated when the student is chosen to build the appropriate options into the drop-down box.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Unable to populate the aasociated values as options in a dropdown
by ww (Archbishop) on Mar 24, 2015 at 04:00 UTC

    Did you think you had entered the Javascript Cloister and addressed your question to the JSgurus?

    What you've shown us barely touches Perl.

    You appear to think your problem is in the JS... so should you not seek solutions in that milieu?



    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.