views:

677

answers:

4

There are some similiar questions posted here, but they are too specific.

When a form has multiple image inputs and the server side uses their names and/or values to distinguish which one was clicked it works perfectly in FF and people often write the whole thing before finding out taht HTML specifies nothing has to be sent and some browsers are not sending it.

The question is: What can I do to fix that?

[edit] This really needs clarification. It's not about sending anything, but sending a pair input_name=input_value (I bolded it in the original question)

The best worst-case scenario example here would be what I've encountered: A list of elements all in one form and all accompanied by buttons with name="delete" value="<item_id>"

A: 

The problem was half solved up to now: like here

But it didn't allow to get the value!

The correct answer is:

$('input[type=image]')
.unbind('mousedown')
.mousedown(function(){ 
  $(this).after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'" />'); 
});

This code creates a hidden duplicate of the input when user starts clicking it. The unbind('mousedown') is to secure it happens once even if You put the code in multiple places in a weird application and it might be called more than once.

I recommend putting it in $(document).ready();

naugtur
I hope this saves some people a lot of rewriting someone else's code.
naugtur
This breaks if JavaScript is not available.
David Dorward
Oh please... When javascript is disabled it changes nothing and nothing breaks. I set up the question and answered it at once. Just wanted to post a solution for people with that same kind of problem, as it was nowhere mentioned in discussions on this topic. Now You people made me want to delete it. Does it bother You that I try to help people dealing with ill-designed application interfaces like one I am dealing with?
naugtur
Oh please. The JS isn't a cosmetic enhancement. It isn't a usability enhancement. The whole point of it is to add data to a form submission that the (badly written) server side script requires and isn't sent, by default, by all browsers. If it doesn't run, the data isn't sent to the server, and it doesn't work. That is "something" and "broken".
David Dorward
You didn't answer the question at the end, but it looks like the answer is yes. If so - vote to delete my answer and I will vote too. I don't have any need to keep this solution here.
naugtur
+2  A: 

Per the HTML spec, clicking on an IMAGE input will return the parameters:

name.x=x-value and name.y=y-value where "name" is the value of the name attribute

with x-value and y-value corresponding to the click position.

Sure, the server code to deal with this will be a little annoying, but you could just check all the query parameter keys with a regular expression:

/^(.*)\.[xy]$/

to search for the IMAGE input keys to determine which IMAGE was clicked.

Dancrumb
This is what I put in my answer in the link to a half-solution. The question is about name AND value. If it wasn't it would be a duplicate question
naugtur
@naugtur: might want to further clarify your question then. At the moment it says "names and/*or* values" and you talk about detecting which image was clicked... which the way I read it, means that this answer is perfectly correct (and the "right" way to do it per the html spec - plus it will work without javascript).
Alconja
@naugatur: it's worth noting that there's a one-to-one mapping between 'name' and 'value'. Depending on your application, it may be sufficient to capture the 'name' from the query-string and use that to infer the 'value' on the server, since the 'value' is not user changeable.Yes... there are some problems with this approach if you have a generic handler for these forms (and so inferring a value from a name is inappropriate)... but it may be appropriate for *some* applications
Dancrumb
If you want to distinguish between inputs, you can given them unique names, i.e. `name="foo" value="bar"` becomes `name="foo_bar" value="bar"`. You can then do something on the server along the lines of `foreach my $key (keys %params) { if ($key =~ /^foo_(.+)\.y$/) { $value = $1; } }`
David Dorward
+1  A: 

I tried with this sample:

<form action="#" method="GET">
  <input type="text" name="t" value="Text here"><br>
  <input type="image" name="a" value="1" src="http://sstatic.net/so/img/logo.png"&gt;&lt;br&gt;
  <input type="image" name="b" value="2" src="http://www.gravatar.com/avatar/c541838c5795886fd1b264330b305a1d?s=32&amp;d=identicon&amp;r=PG"&gt;&lt;br&gt;
</form>

And I get the following urls:

  • FF 3.6: x.html?t=Text+here&b.x=19&b.y=17&b=2#
  • IE 8: x.html?t=Text+here&b.x=22&b.y=18
  • IE 7: x.html?t=Text+here&a.x=185&a.y=51
  • Opera 10: x.html?t=Text+here&a.x=107&a.y=53#
  • Chrome: x.html?t=Text+here&b.x=20&b.y=17&b=2#

So it seems that all the browsers are sending something image related, even if it isn't the image name directly. Since you need to scan for all the image names that you expect to see you can just scan for imagename.x instead. This seems to be how the spec indicates it should work.

Mr. Shiny and New
I bolded the important fact that it's not about sending something. Moreover - I link to an external solution that says image.x and image.y are posted.
naugtur
@naugtur: The spec doesn't work the way you want it to. The simplest thing for you to do is to code your app according to the spec. Which button is clicked is easily detected by looking at the parameter list. You can fake it with javascript but it's simpler if you don't.
Mr. Shiny and New
I'm starting to wish I hadn't posted it... This question is all about a hack that I found to easily fix up applications that someone did for FF only, and now they call You to make it work elsewhere. I wanted to share it, as I didn't find any value-sending solution when I needed and the problem was posted in lots of places. I really don't need that question posted here. I posted it after solving just to help people out.
naugtur
A: 

I have had the same problem

http://stackoverflow.com/questions/3139491/request-params-request-form-not-working-in-internet-explorer

Your answer saved me. God bless you.

Syed Salman Akbar
You should vote up for the naswer, and/or put a comment. Posting a comment as an answer is not a good practice.
naugtur