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.
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'>
<INPUT type='hidden' name='hiddenfield' value='defaultValue'>
... and a form named "formname"
<FORM name="formname"
<FORM name="formname"
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);'>
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&
What is wrong with :
<input type="image" src="someimg.gif" onClick="somefunction()">
Cd&
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?
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.
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.
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>
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> what goes here?
<INPUT type='hidden' name='hiddenfield' value='defaultValue'>
<A href="#"
onClick='document.forms[0] .hiddenfie ld.value=" whatever"; document.f orms[0].su bmit();ret urn 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].hiddenfi eld1.value ="whatever 1";
document.forms[0].hiddenfi eld2.value ="whatever 2";
document.forms[0].hiddenfi eld3.value ="whatever 3";
document.forms[0].submit() ;
}
</script>
<A href="#" onClick='return mySubmit();'>
<IMG name='imgSubmit' src='submit.gif'>
</A>
<INPUT type='hidden' name='hiddenfield' value='defaultValue'>
<A href="#"
onClick='document.forms[0]
<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].hiddenfi
document.forms[0].hiddenfi
document.forms[0].hiddenfi
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;
}
return false;
}
Slightly off topic but what do you mean about type=image not having properties?
Steve
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.len gth); // 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(docu ment.myfor m);'>
<FORM name='myform'>
<INPUT type='image' name='YYZ' src='YYZ.gif' value='insert'>
</form>
</body>
</html>
<HTML>
<HEAD>
<SCRIPT language='javascript'>
function onLoadHandler(theForm)
{
alert(theForm.elements.len
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(docu
<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
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&
<OT>
I haven't checked, but it might show up as an attribute of the submit method of the form.
</OT>
Cd&
onClick='document.formname
<IMG name='imgSubmit' src='submit.gif'>
</A>