Solved

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

Posted on 2002-06-10
16
157 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
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 position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

831 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