Link to home
Start Free TrialLog in
Avatar of VincentLawlor
VincentLawlor

asked on

Image as a submit button

Hi All,

I have a form populated with data from a MySQL database in the form of a table.

Each row in the table has a Description and an edit image.
I want to be able to use the image to submit a value back to my servlet.
Something like below:

<form name="test" action="/blah/blahblah" method="post">
...
...
<%
for(int i = 0; i<somearray.length;i++){
    SomeClass cls = somecollection[i];
%>
<tr>
<td><% cls.getDescription() %></td>
<td><input name="ID" type="image" src="img/edit_img.gif" value="<% cls.getID() %>"  onClick="document.test.cmd.value='update'></td>
</tr>
<%}%>

Everything looks fine the description and image are displayed and the page source looks fine:

<tr>
<td>Something</td>
<td><input name="ID" value="9" type="image" src="img/edit_icon.gif" onClick="document.test.cmd.value='update'"></td>
</tr>

<tr>
<td>Another thing</td>
<td><input name="ID" value="10" type="image" src="img/edit_icon.gif" onClick="document.test.cmd.value='update'"></td>
</tr>

<tr>
<td>Yet another thing</td>
<td><input name="id" value="11" type="image" src="img/edit_icon.gif" onClick="document.test.cmd.value='update'"></td>
</tr>

However when I hit one of the images the parameter ID is not set.
Doing the same thing with a radio input element and a submit button works.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of fargo
fargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VincentLawlor
VincentLawlor

ASKER

It's a hidden input element used to determine the next command the servlet should execute.

So what you are saying is I could/should use something like:

<script type="text/javascript">
function update(cmd,itm){
document.test.cmd.value=cmd;
document.test.item.value=itm;
document.test.submit();
}
</script>

<form name="test" action="/blah/blahblah" method="post">
...
...
<%
for(int i = 0; i<somearray.length;i++){
    SomeClass cls = somecollection[i];
%>
<tr>
<td><% cls.getDescription() %></td>
<td><input name="ID" type="image" src="img/edit_img.gif" value="<% cls.getID() %>"  onClick="update('update',<% cls.getID() %>);"></td>
</tr>
<%}%>
....

<input type="hidden" name="cmd">
<input type="hidden" name="item">
...
exactly
To the rescue again fargo.

Does an image input element not have a value attribute?
image input element does have a value attribute. But when u use image as submit button, the x,y coordinates of the image are submitted. You may use different names for the images and then parse the params to get the value.

In other words, use plain javascript and be happy.
i found one good example and explanation of what i said in my last post (with example)
http://www.jguru.com/faq/view.jsp?EID=87441
Will do.

Again many thanks.