Solved

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

Posted on 2002-06-10
16
158 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

765 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