Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving of Form Items Using Perl and the onchange option. 1

Status
Not open for further replies.

sjg0003

MIS
May 24, 2004
9
CA
I have been looking for a way to save off the value of a drop down list box and use this value to parse data based on the value of that item.

I am using the onchange option on in the <select> however when I change items nothing happens. Can someone please help me figure this out?

Here is a scaled down version of what I am trying to do.

#!/usr/bin/perl
use strict;
print("Content-Type: text/html\n\n");

print "<html>";
print "<head>";

print qq(<script language="javascript">);
print qq(function SetOptions(the_select) {);
my $var1 = print qq(the_select.value);
print "my var: ";
print $var1;
print qq(});
print "</script>";

print "</head><body>";

print qq(<form name="formname">);
print qq( <br> Platform Type: );

print qq(<select name="selectbox1" value style="width:153px" onchange=SetOptions(formname.selectbox1.options.value)">);
print qq(<option name="HP" value="HP">HP</option>);
print qq(<option name="Linux" value="LN">Linux</option>);
print qq(<option name="PC" value="PC">PC</option>);
print qq(<option name="SUN" value="SN">SUN</option>);
print qq(<option name="Other" value="OT">Other</option>);
print qq(</select>);

print qq(</form> </body> </html>);

Thanks
 
AFAIK,
the OnChange event is fired in the javascript while the rendered HTML is in the browser, not when it's been sent back to the server.

You'll need an action for the form
Code:
print qq(<form name="formname" [b]action=/cgi-bin/script2.pl method=POST[/b]>);
print qq( <br> Platform Type: );

print qq(<select name="selectbox1" value style="width:153px" onchange=SetOptions(formname.selectbox1.options.value)">);
        print qq(<option name="HP" value="HP">HP</option>);
        print qq(<option name="Linux" value="LN">Linux</option>);
        print qq(<option name="PC" value="PC">PC</option>);
        print qq(<option name="SUN" value="SN">SUN</option>);
        print qq(<option name="Other" value="OT">Other</option>);
print qq(</select>);
[b]print  qq(<input type=submit>);[/b]
print qq(</form> </body> </html>);
you'll also need the submit button to process the form (ie send it back to the server). Now you need script2.pl to place in the cgi-bin directory of the webserver
Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $query=new CGI;
print $query->header;
my $os=$query->param('selectbox1');
print $query->"the selected OS was $os";

HTH
--Paul

cigless ...
 
When I do it the way you have suggested I recieve the following error:

String found where operator expected at ./script2.pl line 8, near "->"the selected OS was $os""
(Missing operator before "the selected OS was $os"?)
syntax error at ./script2.pl line 8, near "->"the selected OS was $os""
Execution of ./script2.pl aborted due to compilation errors.


 
print "the selected OS was $os";
my bad
--Paul

cigless ...
 
Paul,

Thanks for the help. I was wondering if you coud assist me with another issue that I am having?


I have expanded the script a bit to include a few more list boxes and one listbox that gets dynamically created based on what was selected in the platform listbox. It looks like my dynamically created listbox is not getting any values associated to it, even though there is data within it.

The type of equipment listbox gets generated fine and has all the data that I need. However when I submit the form the data that is contained in the "equipment type" listbox is not available.

script1.html
Code:
<html>
<head>

<script language="javascript">


var HP = new Array("New", "Other");
var LN = new Array("New");
var PC = new Array("New");
var SN = new Array("sys3", "sys2");
var OT = new Array("New");


function SetOptions(the_select, the_array) {
    the_select.options.length = the_array.length;
    for (loop=0; loop < the_array.length; loop++) {
        the_select.options[loop].text = the_array[loop];
    }
}
</script>

</head>
<body>


<form name="formname" action=script2.pl method=POST >

Platform Type:

<select name="platform" value style="width:153px" onchange="SetOptions(formname.type, eval(formname.platform.options[selectedIndex].value))">
        <option name="HP" value="HP">HP</option>
        <option name="Linux" value="LN">Linux</option>
        <option name="PC" value="PC">PC</option>
        <option name="SUN" value="SN">SUN</option>
        <option name="Other" value="OT">Other</option>
</select>


Equipment Type:
<select name="type" value style="width:153px">
<option name="" value=""></option>
</select>

<input type=submit>
</form> </body> </html>

script2.pl
Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $query=new CGI;
print $query->header;
my $os=$query->param('platform');
my $type=$query->param('type');

print "The selected OS was $os\n";
print "The type of equipment is: $type\n\n";
 
This seems to be more of a javascript problem, try this
Code:
function SetOptions(the_select, the_array) {
    the_select.options.length = the_array.length;
    for (loop=0; loop < the_array.length; loop++) {
        the_select.options[loop].text = the_array[loop];
        [b]the_select.options[loop].value = the_array[loop];[/b]
    }
}


Just a stab in the dark, dunno if it'll work, if it doesn't try posting in the javascript forum

HTH
--Paul



cigless ...
 
Nice stab.. That worked..

Thanks for all the help..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top