Solved

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

Posted on 2002-06-10
16
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Hacked 1 71
Can't get cursor to change 3 16
.CSS HTML Help 3 28
title attribute 5 26
This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 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