Link to home
Start Free TrialLog in
Avatar of miredo
miredo

asked on

How do I make an image function as a submit button.

I have an image I would like to use as a submit button for a form.  Also pass hidden variables to the form process.  How do I do that.  I know  Javascript and PHP.  I would prefer that the user not have accessess to the processing code.
Avatar of knightEknight
knightEknight
Flag of United States of America image

<A href="#"
   onClick='document.formname.hiddenfield.value="processingCode";document.formname.submit();return false;'>
<IMG name='imgSubmit' src='submit.gif'>
</A>
then in your form you have a field named "hiddenfield" (or whatever) that you can set to some value for "processingCode"

<INPUT type='hidden' name='hiddenfield' value='defaultValue'>
... and a form named "formname"

<FORM name="formname"
Avatar of miredo
miredo

ASKER

I'm not sure I understand.  Wouldn't the processing code be a script (PHP) or function (JavaScript) that is called when the form is submitted? Here, it looks like  the processing code is assigned to the hidden value. Does that actually call a processing script?  What if I have more than one hidden field?

I mis-understood what you meant by processing code ... I thought you meant the hidden values ... but anyway, the user will not be able to see the processing code on the server, but they will be able to see everything in the browser -- including any values you assign to hidden fields.

The server processing code would be called by the action of your form:

<FORM action='somefile.php' method='post' onSubmit='return validate(this);'>
Why an image link?

What is wrong with :
<input type="image" src="someimg.gif" onClick="somefunction()">

Cd&
Avatar of miredo

ASKER

COBOLdinosaur's solution would work, if the click sends the form field values, and hidden values, to "somefunction()".  Would it?

knightEknight, what does return validate(this) do?

So you're saying I would do

                   <FORM action='somefile.php' method='post' onSubmit='return validate(this);'>
Then how do I get the image to submit when I click on it?
Cd&,
I thought we established in a previous question that the input-type=image has no properties?  does it have an onclick?  if so, then the function "somefunction" could do the same types of things I listed above in the onclick of the <A> tag.  if not, then you could still make a function out of it and call it from the <A> tag.

the onSubmit handler I pasted in the form is generic ... you don't need it, sorry, force of habit.
Avatar of miredo

ASKER

Would someone mind writing out the code:
<FORM action='somefile.php' method='post'>
<INPUT type="text" name="whatever" size="10" maxlength="10">
[...etc..]

then what would the clickable button image line look like
<  what goes here >

</FORM>
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

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
>> what goes here?



<INPUT type='hidden' name='hiddenfield' value='defaultValue'>

<A href="#"
  onClick='document.forms[0].hiddenfield.value="whatever";document.forms[0].submit();return false;'>
<IMG name='imgSubmit' src='submit.gif'>
</A>


if you have more than one hidden field, you should probably use a function:


<SCRIPT language='javascript'>
 function mySubmit()
 {
   document.forms[0].hiddenfield1.value="whatever1";
   document.forms[0].hiddenfield2.value="whatever2";
   document.forms[0].hiddenfield3.value="whatever3";
   document.forms[0].submit();
 }
</script>

<A href="#" onClick='return mySubmit();'>
<IMG name='imgSubmit' src='submit.gif'>
</A>
add this line to the bottom of the function:

   return false;
}
Slightly off topic but what do you mean about type=image not having properties?

Steve
I can't recall the question, but I ran this test to establish that input type=image does not have the same form properties as other input types:



<HTML>
<HEAD>
<SCRIPT language='javascript'>

function onLoadHandler(theForm)
{
  alert(theForm.elements.length);  // shows that the image is not an element of the form

  var s="";
  for ( o in theForm )
     s+=o+", ";
  alert(s);   // shows all other form properties, none of which is the image
}

</script>
</head>

<BODY onLoad='onLoadHandler(document.myform);'>

<FORM name='myform'>
 <INPUT type='image' name='YYZ' src='YYZ.gif' value='insert'>
</form>

</body>
</html>
How wierd (it doesn't appear in the links or images collections either which are the only other two places I can envisage that you could reference the button).

Steve
Glad we could help.  Thanks for the A.:^)


<OT>
  I haven't checked, but it might show up as an attribute of the submit method of the form.
</OT>

Cd&