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

Hello! I just need some inputs regarding a problem I recently saw in my program with regards to the way IE handles the data from the HTML Tag <INPUT type="image">. Here's a portion of my HTML Code:
<TD> <INPUT type="image" name="page" value="1"> <INPUT type="image" name="page" value="2"> <INPUT type="image" name="page" value="3"> <INPUT type="image" name="page" value="4"> ... <INPUT type="image" name="page" value="50"> </TD>
And here's the part of the CGI script that processes after one of the above images is clicked from the browser as the HTML is displayed:

$page = $cgi->param('page') || 1;

Firefox browser works perfect! Every time the image is clicked, the "value" is correctly passed to $page.

However, IE doesn't work the same way. Every time I click the button, it can't seem to find any value being passed, resulting to a value for $page to be always 1. Is <INPUT type="image"> not supported by IE?

Replies are listed 'Best First'.
Re: <INPUT type="image"> problem with IE browser
by bart (Canon) on May 18, 2006 at 09:14 UTC
    Looks like it. HTMLhelp.com doesn't mention a separate parameter for name=value, only name.x and name.y. Neither does the official HTML spec.

    It looks like you'll have to find another way, like making your page number part of the name, such as

    <INPUT type="image" name="page23" src="...">

    A grep through all params with a name matching /^page(\d+)\.x$/ will then find your page number.

    ($param) = grep /^page\d+\.x$/, $cgi->param;
    Or, using map, and the fact that a regex that doesn't match returns an empty list, and a list of captured matches if it does, that can even become:
    ($page) = map /^page(\d+)\.x$/, $cgi->param;

    That doesn't look half bad to me.

Re: <INPUT type="image"> problem with IE browser
by davorg (Chancellor) on May 18, 2006 at 08:47 UTC

    Try replacing the action on the form with a simple CGI parameter dumper like this:

    #!/home/crossda/perl/bin/perl use strict; use warnings; use CGI ':cgi'; print header(-type => 'text/plain'); foreach (param) { print "$_ : ", param($_), "\n"; }

    You'll see that Firefox sends parameters called "page.x", "page.y" and "page". IE doesn't send the "page".

    You'd need to check the specs, but I suspect that IE is doing all it's supposed to do. The extra parameter from Firefox is useful but (I think) non-standard.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Even simpler (= less work), make the form use a GET (don't specify a method for the form) and look at the URL after you submit.
Re: <INPUT type="image"> problem with IE browser
by McDarren (Abbot) on May 18, 2006 at 09:42 UTC
    Actually, after seeing the rest of the replies I've realised that this is something that I had to deal with myself a while back.

    The way I got around it was (as has been suggested) to assign individual names to each INPUT control. In my case, I was using arrow images as sorting buttons. The code to render the HTML was a little like so:

    print qq(<input type="image" name="sort:$field:asc" src="$image_dir/do +wn.png">);
    The above was within a for loop, where the value of $field was changed on each iteration, to produce a unique name for each "button".

    I had about 20 of these little "sorting" buttons on my page, so when it came to working out which one had been clicked (if any), I did this:

    my ($sort_string) = grep { $_ =~ /^sort/ } @params; my ($sort_field, $sort_dir) = defined($sort_string) ? $sort_string =~ /^sort:(\w+):(\w+)/ : qw(avin desc);

    The above worked fine in both IE and Firefox.

    Hope this helps,
    Darren :)

      This one's very simple, yet clever approach. I'll implement this in my subsequent routine when necessary.

    <input type="image" name="submit" src="./images/Login-button.gif" value="Login" alt="Submit Button"/>

    Re: <INPUT type="image"> problem with IE browser
    by holli (Abbot) on May 18, 2006 at 09:04 UTC
      It's a problem with IE. It simply does not send the values of the image buttons. I have a workaround for that, but it relies on Javascript:
      <form action="/blabla" method="post"> <input type="hidden" name="mypage" id="mypage" value="" /> <input type="image" name="page" value="1" src="/static/images/suchen +.gif" onclick="document.getElementById('mypage').value='1';" /> <input type="image" name="page" value="2" src="/static/images/suchen +.gif" onclick="document.getElementById('mypage').value='2';" /> ... </form>
      and later in the script:
      $page = $cgi->param('page') || $cgi->param('mypage') || 1;


      holli, /regexed monk/
        It's a problem with IE

        I think that's overstating the case. IE is doing exactly what it should be doing. Firefox has added some new and useful behaviour. But it's non-standard* and IE can't be expected to support it.

        * Or, at least, I can't find any standards that mandate the Firefox behaviour.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        Thanks for your inputs guys! I made it to work using this workaround:
        <TD> <INPUT type="hidden" name="page"> </TD> <TD> <A href="javascript:do_Submit(1)"><IMG src="page1.gif" ...></A> <A href="javascript:do_Submit(2)"><IMG src="page2.gif" ...></A> <A href="javascript:do_Submit(3)"><IMG src="page3.gif" ...></A> <A href="javascript:do_Submit(4)"><IMG src="page4.gif" ...></A> ... <A href="javascript:do_Submit(50)"><IMG src="page50.gif" ...></A> </TD>
        The on the javascript:
        function do_Submit(page) { var page; document.forms[0].page.value = page; document.forms[0].submit(); }
        I also confirmed that this wasn't supported in the HTML specs. I just assumed too much when I made the code. But I wasn't able to catch it soon enough since I am always using Firefox
    Re: <INPUT type="image"> problem with IE browser
    by ikegami (Patriarch) on May 18, 2006 at 14:56 UTC
      Re^3: <INPUT type="image"> problem with IE browser explained the bug. Here's an untested workaround:
      <button name="page" type="submit" value="1"><img ...></button> <button name="page" type="submit" value="2"><img ...></button> <button name="page" type="submit" value="3"><img ...></button> ... <button name="page" type="submit" value="50"><img ...></button>

        Another bug in IE causes <button> elements to be submitted with the text content the element instead of the value attribute.

        I could be wrong, but I seem to recall they IE also treats all <button>s as successful controls, even if they are unclicked submit buttons.

    Re: <INPUT type="image"> problem with IE browser
    by McDarren (Abbot) on May 18, 2006 at 08:41 UTC
      Update: The below is absolute codswallop. Please see my slightly more sensible reply below

      Try closing the INPUT tag, eg:

      <INPUT type="image" name="page" value="1" />

      --Darren