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

Hi there,

farily simple question, I want to be able to use a piece of artwork for the browse button used in the filefield section of the following script, instead of using the default "Browse..."
Something like the submit button, but I don't know how to implement this for the filefield part. Any help is much appreciated, an example would be great.
#!/usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); #errors will show in the browser my $newStyle; my $co=new CGI; print $co->header, $co->start_html( -title=>'fileupload.cgi', -BGCOLOR=>'#FFFFFF', -style=>{-src=>'http://localhost/html/stylesheet.css', -code=>$newStyle} ), $co->start_multipart_form( -method=>'POST', -action=>"fileupload2.cgi", -enctype=>"multipart/form-data", ), $co->p, $co->filefield( -name => 'file', -default => "starting value", -size => 30, -maxlength => 80, ), http://localhost/html/add.bmp", -value => "starting value", ), $co->p, $co->image_button( -name => "submit", -src => "http://localhost/html/add.bmp", ), $co->end_multipart_form; $co->end_html; exit 0;
Thanks in advance,
Jonathan

Replies are listed 'Best First'.
Re: cgi question - using an image button for filefield
by simon.proctor (Vicar) on Nov 12, 2004 at 15:50 UTC
    To do this go here: Quirks mode css site

    As you will see - its hardly elegant but if that is what you want :).
      Doesn't look elegant at all, is there no other way to do this kind of thing?
      Thanks,
      Jonathan
        Hi Jonathan,

        How about something like this, uses a bit of scripting instead of all that css stuff:
        #!/usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); #errors will show in the browser my $co=new CGI; print $co->header, $co->start_html( -title=>'fileupload.cgi', -BGCOLOR=>'#FFFFFF', ), $co->start_form( -method=>'POST', -name=>"myForm", -action=> "http://localhost/cgi-bin/fileupload2.cgi", ), $co-> filefield( -name => 'browse', -style => 'display:none' ), $co-> textfield( -name => 'file', ), "&nbsp;&nbsp;&nbsp;&nbsp;", "<a href=\"#\" onClick=\"document.forms[0].browse.click();document.for +ms[0].file.value=document.forms[0].browse.value;return false;\">", "<img name=\"browse\" src=\"http://localhost/html/browse.bmp\" alt=\"b +rowse\" border=\"0\"></a>", $co->p, $co->image_button( -name => 'submit', -src => 'http://localhost/html/add.bmp', -value => "Submit", -onclick => "document.forms[0].browse.disabled=tru +e;document.forms[0].submit()", ), $co->end_form(), $co->end_html; exit 0;
        I've tried this myself and it does appear to work.
        Enjoy,
        Martymart