http://www.perlmonks.org?node_id=456197

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

Fellow Monasterians,

While I have seen this addressed indirectly in various nodes, I haven't been able to find anything definitive.

Here's the scenario: a user selects an option from a pulldown menu and submits a form. My script validates the input, finds a problem, and re-outputs the .tmpl page with the error message.

The associate => $query re-populates text boxes, but I have to rely on some hand-rolled code to re-set any dropdowns.

The code consists of: 1) 'onsubmit' Java-the-script to populate a like-number of hidden fields with the indexes of the options selected; 2) then Perl to grab those and pass them back to H::T if necessary (an error is found); and finally, 3) more JS to reset the dropdowns 'onload'ing. Messy, indeed. Question: What is the better, more Perlish, way? Thanks!

ON SUBMIT: <form id="main" action="cgi-bin/script.pl" method="post" onsubmit="rem +ember_indexes()"> <input type="hidden" id="tmpidx" name="tmpidx" value="" /> <input type="hidden" id="styidx" name="styidx" value="" /> function remember_indexes() { var tmpidx = document.getElementById("templatest").selectedIndex; var styidx = document.getElementById("stylest").selectedIndex; document.getElementById("tmpidx").value = tmpidx; document.getElementById("styidx").value = styidx; }</blockquote> ON RETURNING AFTER ERROR IS FOUND: Perl: my $styidx = $query->param('styidx'); my $tmpidx = $query->param('tmpidx'); $template -> param( errormsg => $errormessage, styidx => $styidx, tmpidx => $tmpidx); HTML: function set_dropdowns() { document.getElementById("styles").options[<tmpl_var styidx>].select +ed = true; document.getElementById("templates").options[<tmpl_var tmpidx>].sel +ected = true;} <body onload="set_dropdowns();"> <p><tmpl_var errormsg><p> <select name="templates" id="templates"> <option value="">Select...</option> <option value="23>">Text one</option> <option value="36>">Text two</option> </select>

Update: fixed tags


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot