Solved

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

Posted on 2002-06-10
16
152 Views
Last Modified: 2010-04-09
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.
0
Comment
Question by:miredo
  • 8
  • 3
  • 3
  • +1
16 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7067791
<A href="#"
   onClick='document.formname.hiddenfield.value="processingCode";document.formname.submit();return false;'>
<IMG name='imgSubmit' src='submit.gif'>
</A>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7067798
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'>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7067801
... and a form named "formname"

<FORM name="formname"
0
 

Author Comment

by:miredo
ID: 7068061
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?

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7068089
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);'>
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7068144
Why an image link?

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

Cd&
0
 

Author Comment

by:miredo
ID: 7068197
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?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7068499
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:miredo
ID: 7069137
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>
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 100 total points
ID: 7069604
kEk,

No properties, but it does support events just like a submit.  Only problem is Netscrap 4.x (What else is new?) which does not suppor on Click for it.

miredo,

<FORM action='somefile.php' method='post'>
<INPUT type="text" name="whatever" size="10" maxlength="10">
[...etc..]
hidden fields go here
<input type="image" src="yourimage.gif" onClick="your function()">
</form>

The function can contain whatever code you are using to populate the hidden fields and should end with:
document.forms[0].submit();


If you have to support netscrap 4 then it would have to be:

<FORM action='somefile.php' method='post'>
<INPUT type="text" name="whatever" size="10" maxlength="10">
[...etc..]
hidden fields here
<a href="JavaScript:;" onClick="yourfunction()"><img src="yourimage.gif"></a>

Cd&
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7069750
>> 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>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7069757
add this line to the bottom of the function:

   return false;
}
0
 
LVL 11

Expert Comment

by:mouatts
ID: 7070154
Slightly off topic but what do you mean about type=image not having properties?

Steve
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7070169
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>
0
 
LVL 11

Expert Comment

by:mouatts
ID: 7070270
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
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7071094
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&
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Do you want to insert HTML5 video into your site? This is the tutorial how to do so. What are the main advantages of HTML5 video? 1) Have good compression, good image quality, and low decode processor use. 2) It is royalty-free 3) It is easi…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now